iOS应用管理(优化)

//

//  ViewController.m

//  01-应用管理

//  Created by apple on 17-5-14.

//  Copyright (c) 2017  All rights reserved.

//

 

#import "ViewController.h"

 

@interface ViewController ()

@property(nonatomic,strong)NSArray *apps;

 

@end

 

@implementation ViewController

- (NSArray *)apps{

    

    if (_apps == nil) {

//        1.获取plis文件的路径

     NSString *path =   [[NSBundle mainBundle]pathForResource:@"app.plist" ofType:nil];

//        2.读取文件

       _apps =  [NSArray arrayWithContentsOfFile:path];

        

    }

    return  _apps;

}

 

- (void)viewDidLoad {

    [super viewDidLoad];

//    1.设置frame的相关属性

    CGFloat appViewW = 100;

    CGFloat appViewH = 120;

    CGFloat spaceX = 20;

    CGFloat spaceY = 30;

    CGFloat topMargin = 30;

    CGFloat leftMargin = ( self.view.frame.size.width - 3 * appViewW - 2 *spaceX) *0.5;

//    2.创建格子

    for (int i = 0;  i < self.apps.count; i ++) {

//        2.1创建一个格子视图

        UIView *appView = [[UIView alloc]init];

 

        int row = i /3;//行号

        int col = i % 3;//列号

        CGFloat appViewX = leftMargin + (appViewW + spaceX)*col;

        CGFloat appViewY = topMargin + (appViewH + spaceY )*row;

//        2.2设置frame

        appView.frame = CGRectMake(appViewX, appViewY, appViewW, appViewH);

//        2.3添加

        

        [self.view addSubview:appView];

//        appView.backgroundColor = [UIColor redColor];//设置背景色

        NSDictionary *dict = self.apps[i];

//        2.4添加图片

        UIImageView *head = [[UIImageView alloc]init];

        CGFloat headW = 60;

        CGFloat headX = (appViewW - headW) *0.5;

        CGFloat headY = 0;

        CGFloat headH = 60;

        head.image = [UIImage imageNamed:dict[@"icon"]];

        

        head.frame = CGRectMake(headX, headY, headW, headH);

        [appView addSubview:head];

//        head.backgroundColor = [UIColor blueColor];

        

//        2.5添加label

//         2.5.1 创建一个文本标签

        UILabel *nameLabel = [[UILabel alloc]init];

        CGFloat nameLabelX = 0;

        CGFloat nameLabelY = headY + headH;

        CGFloat nameLabelW = appViewW;

        CGFloat nameLabelH = 30;

        

        nameLabel.frame = CGRectMake(nameLabelX, nameLabelY, nameLabelW, nameLabelH);

        [appView addSubview:nameLabel];

//        nameLabel.backgroundColor = [UIColor grayColor];

        nameLabel.text = dict[@"name"];

//       2.5.2设置文字大小

        nameLabel.font = [UIFont systemFontOfSize:13];

//        2.5.3设置文字居中

        nameLabel.textAlignment = NSTextAlignmentCenter;

 

//        2.6添加button

        UIButton *downLoadBtn = [[UIButton alloc]init];

        CGFloat downLoadBtnX = 10;

        CGFloat downLoadBtnY = nameLabelY + nameLabelH + 10;

        CGFloat downLoadBtnW = appViewW - downLoadBtnX *2;

        CGFloat downLoadBtnH = 30;

        

        downLoadBtn.frame = CGRectMake(downLoadBtnX, downLoadBtnY, downLoadBtnW, downLoadBtnH);

        [appView addSubview:downLoadBtn];

//        downLoadBtn.backgroundColor = [UIColor greenColor];//设置背景色

        

        [downLoadBtn setTitle:@"下载" forState:UIControlStateNormal];//设置文字

        [downLoadBtn setBackgroundImage:[UIImage imageNamed:@"buttongreen"] forState:UIControlStateNormal];

        [downLoadBtn setBackgroundImage: [UIImage imageNamed:@"buttongreen_highlighted"] forState:UIControlStateHighlighted];

//        downLoadBtn.

//        按钮内部至少有两个控件,一个label,一个imageView,

        downLoadBtn.titleLabel.font = [UIFont systemFontOfSize:14];

        

 

 

    }

    

    

    

    

  

}

 

@end

 

posted @ 2017-08-02 16:57  李洪强  阅读(145)  评论(0编辑  收藏  举报