// // ViewController.m // 应用管理 // // Created by YaguangZhu on 15/7/31. // Copyright (c) 2015年 YaguangZhu. All rights reserved. // #import "ViewController.h" @interface ViewController () @property (nonatomic,strong) NSArray *apps; @end @implementation ViewController - (NSArray *)apps { if (_apps == nil) { NSString *path = [[NSBundle mainBundle] pathForResource:@"app.plist" ofType:nil]; NSArray *array = [NSArray arrayWithContentsOfFile:path]; _apps =array; } return _apps; } - (void)viewDidLoad { [super viewDidLoad]; int colums =3; CGFloat viewWidth = self.view.frame.size.width; CGFloat appW = 75; CGFloat appH = 90; CGFloat marginTop = 30; CGFloat maginX = (viewWidth - colums*appW)/ (colums+1); CGFloat maginY = maginX; for (int i=0;i<self.apps.count;i++)//9 = self.apps.count { NSDictionary *appDict = self.apps[i]; UIView *appview = [[UIView alloc] init]; // appview.backgroundColor = [UIColor redColor]; int colIdx = i % colums; int rowIdx = i / colums; CGFloat appX = maginX+ colIdx *(appW +maginX); CGFloat appY = marginTop +rowIdx *(appH +maginY); appview.frame = CGRectMake(appX, appY, appW, appH); [self.view addSubview:appview]; // 增加子控件 UIImageView *imgViewIcon = [[UIImageView alloc]init]; // imgViewIcon.backgroundColor = [UIColor blueColor]; CGFloat iconW = 45; CGFloat iconH =45; CGFloat iconX = (appview.frame.size.width- iconW) *0.5; CGFloat iconY = 0; imgViewIcon.frame = CGRectMake(iconX, iconY, iconW, iconH); [appview addSubview:imgViewIcon]; imgViewIcon.image = [UIImage imageNamed:appDict[@"icon"]]; UILabel *lblName = [[UILabel alloc] init]; //lblName.backgroundColor = [UIColor yellowColor]; CGFloat nameW = appview.frame.size.width; CGFloat nameH = 20; CGFloat nameY= iconH; CGFloat nameX = 0; lblName.frame = CGRectMake(nameX, nameY, nameW, nameH); [appview addSubview:lblName]; UIButton *btnDowload = [[UIButton alloc] init]; btnDowload.backgroundColor = [UIColor blackColor]; lblName.text = appDict[@"miaoshu"]; lblName.font = [UIFont systemFontOfSize:14]; lblName.textAlignment = NSTextAlignmentCenter; CGFloat btnW =iconW; CGFloat btnH = 20; CGFloat btnX = iconX; //CGFloat btnY = nameY + nameH; CGFloat btnY = CGRectGetMaxY(lblName.frame); btnDowload.frame = CGRectMake(btnX, btnY, btnW, btnH); [appview addSubview:btnDowload]; [btnDowload setTitle:@"xia zai" forState:UIControlStateNormal]; [btnDowload setTitle:@"finsh" forState:UIControlStateHighlighted]; [btnDowload setBackgroundImage:[UIImage imageNamed:@"12"] forState:UIControlStateNormal]; [btnDowload setBackgroundImage:[UIImage imageNamed:@"10"] forState:UIControlStateHighlighted]; btnDowload.titleLabel.font = [UIFont systemFontOfSize:14]; //按钮的单击事件 [btnDowload addTarget:self action:@selector(btnDownClick) forControlEvents:UIControlEventTouchUpInside]; } } - (void)btnDownClick { NSLog(@"ssss"); } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } @end