标签:
1 - (void)viewDidLoad 2 { 3 [super viewDidLoad]; 4 5 // 搭建界面,九宫格 6 #define kAppViewW 80 7 #define kAppViewH 90 8 #define kColCount 3 9 #define kStartY 20 10 11 // 320 - 3 * 80 = 80 / 4 = 20 12 CGFloat marginX = (self.view.bounds.size.width - kColCount * kAppViewW) / (kColCount + 1); 13 CGFloat marginY = 10; 14 15 for (int i = 0; i < 12; i++) { 16 // 行 17 // 0, 1, 2 => 0 18 // 3, 4, 5 => 1 19 int row = i / kColCount; 20 21 // 列 22 // 0, 3, 6 => 0 23 // 1, 4, 7 => 1 24 // 2, 5, 8 => 2 25 int col = i % kColCount; 26 27 CGFloat x = marginX + col * (marginX + kAppViewW); 28 CGFloat y = kStartY + marginY + row * (marginY + kAppViewH); 29 30 UIView *appView = [[UIView alloc] initWithFrame:CGRectMake(x, y, kAppViewW, kAppViewH)]; 31 // appView.backgroundColor = [UIColor redColor]; 32 [self.view addSubview:appView]; 33 34 // 实现视图内部细节 35 // NSDictionary *dict = self.appList[i]; 36 HMAppInfo *appInfo = self.appList[i]; 37 38 // 1> UIImageView 39 UIImageView *icon = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, kAppViewW, 50)]; 40 // icon.backgroundColor = [UIColor greenColor]; 41 42 // 设置图像 43 // icon.image = [UIImage imageNamed:dict[@"icon"]]; 44 icon.image = [UIImage imageNamed:appInfo.icon]; 45 46 // 设置图像填充模式,等比例显示(CTRL+6) 47 icon.contentMode = UIViewContentModeScaleAspectFit; 48 49 [appView addSubview:icon]; 50 51 // 2> UILabel -> 应用程序名称 52 // CGRectGetMaxY(frame) = frame.origin.y + frame.size.height 53 UILabel *lable = [[UILabel alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(icon.frame), kAppViewW, 20)]; 54 // lable.backgroundColor = [UIColor blueColor]; 55 56 // 设置应用程序名称 57 // lable.text = dict[@"name"]; 58 lable.text = appInfo.name; 59 60 // 设置字体 61 lable.font = [UIFont systemFontOfSize:13.0]; 62 lable.textAlignment = NSTextAlignmentCenter; 63 64 [appView addSubview:lable]; 65 66 // 3> UIButton -> 下载按钮 67 UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(lable.frame), kAppViewW, 20)]; 68 button.backgroundColor = [UIColor yellowColor]; 69 70 // 背景图片 71 [button setBackgroundImage:[UIImage imageNamed:@"buttongreen"] forState:UIControlStateNormal]; 72 [button setBackgroundImage:[UIImage imageNamed:@"buttongreen_highlighted"] forState:UIControlStateHighlighted]; 73 74 // 按钮都是有状态的,不同状态可以对应不同的标题 75 [button setTitle:@"下载" forState:UIControlStateNormal]; 76 // *** 一定不要使用以下方法,修改按钮标题 77 // button.titleLabel.text = @"aaa"; 78 79 // 修改字体(titleLabel是只读的) 80 // readonly表示不允许修改titleLabel的指针,但是可以修改label的字体 81 // 提示:按钮的字体是不区分状态的! 82 button.titleLabel.font = [UIFont systemFontOfSize:12.0]; 83 84 [appView addSubview:button]; 85 } 86 }
标签:
原文地址:http://www.cnblogs.com/xiyang1/p/4531719.html