iOS-创建9宫格代码

- (void)rankWithTotalColumns:(int)totalColumns andWithAppW:(int)appW andWithAppH:(int)appH{
    //总列数
    int _totalColumns = totalColumns;
    //view尺寸
    CGFloat _appW = appW;
    CGFloat _appH = appH;
    
    //横向间隙 (控制器view的宽度 - 列数*应用宽度)/(列数 + 1)
    CGFloat margin = (self.view.frame.size.width - (_totalColumns * 85)) / (_totalColumns + 1);
    
//    for (int index = 0; index < self.appsArray.count; index++) {
    for (int index = 0; index < 5; index++) {
        //创建一个小框框//
        UIView *appView = [[UIView alloc] init];
        appView.backgroundColor = [UIColor redColor];
        //创建结束//
        
        //计算框框的位置...行号列号从0开始
        //行号
        int row = index / totalColumns; //行号为框框的序号对列数取商
        //列号
        int col = index % totalColumns; //列号为框框的序号对列数取余
        // 每个框框靠左边的宽度为 (平均间隔+框框自己的宽度)
        CGFloat appX = margin + col * (appW + margin);
        // 每个框框靠上面的高度为 平均间隔+框框自己的高度
        CGFloat appY = 30 + row * (appH + margin);
        
        appView.frame = CGRectMake(appX, appY, _appW, _appH);
        
        [self.view addSubview:appView];
    }
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 175,046评论 25 709
  • 一直想写点什么,记录下这几个月做产品的心得。借着简书这个平台,写点东西呗。 2014年4月份加入ZAK...
    郑宏远阅读 4,138评论 1 11
  • 今天被朋友叫来发传单。 以前也发过一次传单,,不过那次根本不算发传单,因为那些传单我都没发就全扔了。但这一次却是真...
    云_海_风阅读 1,339评论 0 1