标签:
1 2 // 按钮 3 // 数据 4 NSArray *images = @[@"publish-video", @"publish-picture", @"publish-text", @"publish-audio", @"publish-review", @"publish-offline"]; 5 NSArray *titles = @[@"发视频", @"发图片", @"发段子", @"发声音", @"审帖", @"离线下载"]; 6 7 // 一些参数 8 NSUInteger count = images.count; 9 int maxColsCount = 3; // 一行的列数 10 NSUInteger rowsCount = (count + maxColsCount - 1) / maxColsCount; 11 12 // 按钮尺寸 13 CGFloat buttonW = [UIScreen mainScreen].bounds.size.width / maxColsCount; 14 CGFloat buttonH = buttonW * 0.85; 15 CGFloat buttonStartY = ([UIScreen mainScreen].bounds.size.height - rowsCount * buttonH) * 0.5; 16 for (int i = 0; i < count; i++) { 17 // 创建、添加 18 XMGPublishButton *button = [XMGPublishButton buttonWithType:UIButtonTypeCustom]; 19 [button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside]; 20 [self.view addSubview:button]; 21 22 // frame 23 CGFloat buttonX = (i % maxColsCount) * buttonW; 24 CGFloat buttonY = buttonStartY + (i / maxColsCount) * buttonH; 25 button.frame = CGRectMake(buttonX, buttonY, buttonW, buttonH); 26 27 // 内容 28 [button setImage:[UIImage imageNamed:images[i]] forState:UIControlStateNormal]; 29 [button setTitle:titles[i] forState:UIControlStateNormal]; 30 }
1 var images: Array<String> = ["publish-video", "publish-picture", "publish-text", "publish-audio", "publish-review", "publish-offline"] 2 var titles: Array<String> = ["发视频", "发图片", "发段子", "发声音", "审帖", "离线下载"] 3 4 /** #数组的创建 5 * var s1:Array<String> = ["1", "2", "3"] 6 * var s2:[String] = ["1", "2", "3"] //let 7 * var s3 = [String]() 8 */ 9 var col: NSInteger = 3 10 var count: NSInteger = 6 11 12 let totalloc:Int = 3 13 var viewW:CGFloat = self.view.frame.size.width / CGFloat(col) 14 var viewH:CGFloat = viewW + 15 15 16 for var i = 0; i < count; i++ 17 { 18 var row:Int = i / col 19 var loc:Int = i % col 20 21 var viewX:CGFloat = viewW * CGFloat(loc) + 30 22 var viewY:CGFloat = viewH * CGFloat(row) + 200 23 24 var publish:iCocosPublishButton = iCocosPublishButton(frame: CGRectMake(viewX - 0.5, viewY - self.view.frame.size.height, viewW - 1, viewH)) 25 26 self.view.addSubview(publish) 27 28 publish.setImage(UIImage(named: images[i]), forState: UIControlState.Normal) 29 publish.setTitle(titles[i], forState: UIControlState.Normal) 30 31 publish.tag = i 32 publish.addTarget(self, action: "publishButtonClick:", forControlEvents: UIControlEvents.TouchUpInside) 33 34 self.buttons.addObject(publish) 35 } 36 37 }
版权声明:欢迎转载,请贴上源地址: http://www.cnblogs.com/iCocos/(iOS梦工厂)
更多精彩请关注github:https://github.com/al1020119?tab=repositories
iOS开发——完整项目实战OC篇&项目总结之九宫格布局双语实现
标签:
原文地址:http://www.cnblogs.com/iCocos/p/4799482.html