ios 通过代码添加控件

//1.从 app.plist中加载数据
@property (nonatomic,strong)NSArray * appInfos;


- (NSArray *)appInfos
{
    if(_appInfos == nil)
    {
        //1.1 获取bundle
        NSBundle * bundle = [NSBundle mainBundle];
        //1.2 获取plsit路径
        NSString * path = [bundle pathForResource:@"app" ofType:@"plist"];
        //1.3 加载plist
        _appInfos = [NSArray arrayWithContentsOfFile:path];
    }
    return _appInfos;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    //2.测试数据
    NSLog(@"%@",self.appInfos);
    
    //3.动态生成9宫格
    for (int i = 0; i < self.appInfos.count; i++) {
        //3.1动态生成view
        UIView * subView = [[UIView alloc] init];
        //3.2把生成的subView添加到主界面
        [self.view  addSubview:subView];
        
        //3.3计算 frame
        CGFloat subViewW = 100; //宽
        CGFloat subViewH = 100; //高
        //x轴的横向间距
        CGFloat marginX = (self.view.frame.size.width - 3*subViewW)/4;
        //y轴的纵向间距
        CGFloat marginY = 20;
        //当前view的行号
        int row = i /3;
        //当前view的列号
        int column = i %3;
        //subView的x点坐标
        CGFloat subViewX = marginX + column*(marginX + subViewW);
        //subView的y点坐标
        CGFloat subViewY = 30  + row*(marginY + subViewH);
        
        subView.frame = CGRectMake(subViewX, subViewY, subViewW, subViewH);
        //设置subView的背景色
//        subView.backgroundColor = [UIColor redColor];
        
        
        //获取当前遍历得到的数据
        NSDictionary * appInfo = self.appInfos[i];
        
        //4.生成3个字控件
        //4.1 iconView
        UIImageView * iconView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:appInfo[@"icon"]]];
        [subView addSubview:iconView]; //添加到subview上面
        //计算frame
        CGFloat iconW = 60;
        CGFloat iconH = 60;
        CGFloat iconY = 0;
        CGFloat iconX = (subViewW - iconW)/2;
        iconView.frame = CGRectMake(iconX, iconY, iconW, iconH);
        
        //4.2 nameView
        UILabel * nameView = [[UILabel alloc] init];
        [subView addSubview:nameView]; //添加到subView上面
        
        nameView.text = appInfo[@"name"];
        nameView.font = [UIFont systemFontOfSize:15]; //设置字体大小
        nameView.textAlignment = NSTextAlignmentCenter;//文字居中
        
        
        //计算frame
        CGFloat nameW = subViewW;
        CGFloat nameH = 20;
        CGFloat nameX = 0;
        CGFloat nameY = iconH; //紧挨着icon的底部
        nameView.frame = CGRectMake(nameX, nameY, nameW, nameH);
        
        //4.3下载按钮
        UIButton * downloadView = [UIButton buttonWithType:UIButtonTypeCustom];
        [subView addSubview:downloadView];//添加到subView
        //计算frame
        CGFloat downloadW = iconW;
        CGFloat downloadH = 20;
        CGFloat downloadX = iconX;
        //获取label的最大y值
        CGFloat downloadY = CGRectGetMaxY(nameView.frame);
        downloadView.frame = CGRectMake(downloadX, downloadY, downloadW, downloadH);
        
        //设置文字
        [downloadView setTitle:@"下载" forState:UIControlStateNormal];
        
        //设置背景图片
        [downloadView setBackgroundImage:[UIImage imageNamed:@"buttongreen"] forState:UIControlStateNormal];
        [downloadView setBackgroundImage:[UIImage imageNamed:@"buttongreen_heighted"] forState:UIControlStateHighlighted];
        
        //设置按钮文字的大小
        downloadView.titleLabel.font = [UIFont systemFontOfSize:15];
        
    }
}



你可能感兴趣的:(ios 通过代码添加控件)