适合所有尺寸打印马赛克
CGFloat width = 40; CGFloat height = 40; //获取屏幕宽高 //获取屏幕对象 UIScreen *screen = [UIScreen mainScreen]; //获取屏幕大小 CGRect screenFrame = [screen bounds]; //单独取出屏幕的宽高 // CGFloat screenWidth = screenFrame.size.width; CGFloat screenWidth = CGRectGetWidth(screenFrame); // CGFloat screenHeight = screenFrame.size.height; CGFloat screenHeight = CGRectGetHeight(screenFrame); // x轴 y轴 // CGRectGetMinX + CGRectGetMinY = view的左上角坐标 // CGRectGetMidX + CGRectGetMidY = view的中心坐标 // CGRectGetMaxX + CGRectGetMaxY = view的右上角坐标 // CGRectGetWidth + CGRectGetHeight = view的大小 /* (minx/y) (midx/0) (maxx,y) +-----.-----+ | | . . . (midx, midy) | | +-----.-----+ */ //计算每一列的个数 CGFloat col = screenWidth / width; //计算每一行的个数 CGFloat row = screenHeight / height; for (NSInteger i = 0; i < row; ++i) { for (NSInteger j = 0; j < col; ++j) { UIView *v = [[UIView alloc] init]; //设置第i行,第j列个view的大小 v.frame = CGRectMake(j * width, i * height, width, height); //CGRectGetMidX(v.frame); //(v.frame.origin.x + v.frame.size.width) / 2; //CGRectGetMidY(v.frame); //(v.frame.origin.y + v.frame.size.height) / 2; //设置颜色 v.backgroundColor = [UIColor colorWithRed:arc4random() % 256 / 255.0 green:arc4random() % 256 / 255.0 blue:arc4random() % 256 / 255.0 alpha:1]; //放到窗口显示 [self.window addSubview:v]; } }