iOS开发:九宫格灵活布局
来源:互联网 发布:外卖软件开发 编辑:程序博客网 时间:2024/06/14 06:51
实现一行不同格子数的九宫格布局。
预览
思路
- 手动添加UI控件
- 用除法和求余运算计算格子坐标
- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. //parse the json file NSString* path = [[NSBundle mainBundle] pathForResource:@"LifeService" ofType:@"json"]; NSData *jsonData = [[NSData alloc] initWithContentsOfFile:path]; NSError *error; id jsonObj = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:&error]; if (!jsonObj || error) NSLog(@"JSON parse failed!"); self.squareData = [jsonObj objectForKey:@"Record"]; //set view background color self.view.backgroundColor = [UIColor lightGrayColor]; //setup the square layout for(int i = 0; i < square_count; i++) { //comput the row and col index int row = i/col_count; int col = i%col_count; //comput the size and position float block_size = (self.view.frame.size.width - block_space*col_count)/col_count; float x = block_space/2 + col*(block_size + block_space); float y = TOP_MARGIN + block_space/2 + row*(block_size + block_space); // NSLog(@"%f %f", x, y); UIView *block = [[UIView alloc] init]; block.frame = CGRectMake(x, y, block_size, block_size); block.backgroundColor = [UIColor whiteColor]; UIButton *button = [[UIButton alloc] init]; button.frame = CGRectMake(0, 0, block_size, block_size); //set button image [button setImage:[UIImage imageNamed:[self.squareData[i] objectForKey:@"imgLogo"]] forState:UIControlStateNormal]; [button addTarget:self action:@selector(onClick:) forControlEvents:UIControlEventTouchUpInside]; //set button title [button setTitle:[self.squareData[i] objectForKey:@"title"] forState:UIControlStateNormal];// [button setBackgroundColor:[UIColor greenColor]]; [block addSubview:button]; //add the button to the block [self.view addSubview:block]; }}
源代码下载
csdn:九宫格
github:SquareLayout
0 0
- iOS开发:九宫格灵活布局
- 【iOS开发系列】九宫格布局
- iOS开发中的九宫格布局
- iOS九宫格布局
- ios-九宫格布局
- iOS九宫格/布局算法
- ios 集合视图(九宫格布局)
- iOS UI设计---九宫格布局算法
- ios开发之UI基础--应用管理xib-九宫格布局
- iOS开发 - Xib 九宫格应用管理
- iOS开发-简单制作九宫格拼图
- 九宫格布局
- 九宫格布局
- 九宫格布局练习
- 九宫格布局
- 九宫格布局
- 九宫格基本布局
- 九宫格布局
- 多线程基础
- 学习项目代码总结(一)—— ListView的自定义Adapter的性能提升与注意事项
- android UI——分组+悬停 listview
- 第一次机房收费系统——写给自己
- LeetCode 115 Distinct Subsequences--In C++
- iOS开发:九宫格灵活布局
- jquery中如何防止冒泡事件
- 剑指offer—编程题14(调整数组顺序使奇数位于偶数前面)
- 合理利用手机网站生成工具
- 基于Netty4的HttpServer和HttpClient的简单实现
- CSS3边框
- sphinx检索mysql问题
- Android Studio的快捷键以及设置方式
- Eclipse中将一个android项目当做另外一个android项目的library