iOS for循环创建九宫格
来源:互联网 发布:美国先锋集团 知乎 编辑:程序博客网 时间:2024/05/16 12:59
九宫格在iOS项目UI中是经常会出现的,在这里写了一个简单的for循环创建九宫格,供新手参考。
#pragma mark - createNineSquare- (void)createNineSquare { UIView *btnViewBackground = [[UIView alloc] initWithFrame:CGRectMake(0, bannerImageView.size.height, kDeviceWidth, 3*(kDeviceWidth-3)/4)]; btnViewBackground.backgroundColor = [UIColor lightGrayColor]; [self.view addSubview:btnViewBackground]; CGFloat btnMargin = 1; //间隔 CGFloat btnWidth = (kDeviceWidth-3)/4; //宽 CGFloat btnHeight = (kDeviceWidth-3)/4; //高 for (int i = 0; i<12; i++) { int row = i/4; //行 int col = i%4; //列 btnView = [[UIView alloc] init]; btnView.frame = CGRectMake(col*(btnWidth + btnMargin), row*(btnHeight + btnMargin), btnWidth, btnHeight); btnView.backgroundColor = [UIColor whiteColor]; [btnViewBackground addSubview:btnView]; UIImageView *btnImageView = [[UIImageView alloc] initWithFrame:CGRectMake(btnWidth/3, 15, btnWidth/3, btnWidth/3)]; btnImageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"%@",self.btnImageArray[i]]]; [btnView addSubview:btnImageView]; UILabel *btnLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, btnWidth/3+25, btnWidth, 30)]; btnLabel.backgroundColor = [UIColor clearColor]; btnLabel.textAlignment = NSTextAlignmentCenter; btnLabel.textColor = [UIColor blackColor]; btnLabel.text = [NSString stringWithFormat:@"%@",self.btnLabelTitleArray[i]]; btnLabel.font = kSystemFont_14; [btnView addSubview:btnLabel]; button = [UIButton buttonWithType:UIButtonTypeCustom]; button.frame = CGRectMake(col*(btnWidth + btnMargin), row*(btnHeight + btnMargin), btnWidth, btnHeight); button.backgroundColor = [UIColor clearColor]; button.tag = 1000+i; [button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside]; [btnViewBackground addSubview:button]; } }
这是一个简单的九宫格类型的Button 以及对应的图片和标题。
0 0
- iOS for循环创建九宫格
- iOS 用For循环实现九宫格的实现
- 两种方法使用for循环动态创建按钮(类似九宫格)
- IOS开发for循环创建button
- ios批量创建UIImageView,微博九宫格图片展示
- 快速创建九宫格
- CSStask1-创建九宫格
- IOS之九宫格
- ios九宫格实现
- iOS实现九宫格
- iOS九宫格布局
- iOS- 九宫格解锁
- ios九宫格
- IOS九宫格
- ios 九宫格计算
- ios-九宫格布局
- iOS 九宫格
- iOS 简单九宫格
- 在线作图工具
- svn目录 部分更新
- 深度学习2015年文章整理(CVPR2015)
- 算法——排序之路(二)——希尔排序法(Shell Sort)
- 参数数目不定的函数
- iOS for循环创建九宫格
- 自定义一些常见的Dialog效果
- SQLCookBook第三章学习日记7
- 针对Servlet的线程安全问题
- C++内存泄露问题
- JS正则表单邮箱验证实例(错误信息周时显示出来)
- 自我介绍
- java基础(五)------ Java程序中初始化的顺序
- 知乎上的IT人员状态