标签:
// 九宫格
#import "ViewController.h" @interface ViewController () @property (nonatomic ,strong) NSArray *apps; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // 1.总列数 int totalColums = 3 ; // 2.应用的宽度高度 CGFloat appW = 85 ; CGFloat appH = 85 ; // 3.间隙 = (view宽度 - (列数*宽度)) / (总列数+1) CGFloat margin = (self.view.frame.size.width - totalColums *appW)/(totalColums +1); // 4.根据应用个数创建对应的框框 for (int index =0; index<self.apps.count; index++) { // 4.1 创建格子 UIView *appView = [[UIView alloc ]init ]; // 4.2 设置格子的颜色 appView.backgroundColor = [UIColor redColor]; // 计算行号和列号 int row = index / totalColums; int col = index % totalColums; // 计算格子的位置 CGFloat appX = margin +col *(appW+margin); CGFloat appY = 30 +row*(appH+margin); appView.frame = CGRectMake(appX, appY, appW, appH); // 加载 [self.view addSubview:appView]; } } - (NSArray *)apps{ if(_apps == nil){ // 1.获取plist的全路径 NSString *path = [[ NSBundle mainBundle] pathForResource:@"app.plist" ofType:nil]; // 2. 加载数组 _apps = [NSArray arrayWithContentsOfFile:path]; } return _apps; }
标签:
原文地址:http://www.cnblogs.com/jerry1209/p/4272851.html