UIView 中布局学习例子

3*4式内容显示

@implementation ViewController
-(NSArray *)apps{
    if (_apps==nil) {
        NSString *path=[[NSBundle mainBundle] pathForResource:@"app.plist" ofType:nil];
        _apps=[NSArray arrayWithContentsOfFile:path];
    }
    return _apps;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    //设置相关大小宽度
    int appCount=3;
    CGFloat appH=120;
    CGFloat appW=110;
    //计算间隙距离
    CGFloat margin=(self.view.frame.size.width-appW * appCount)/(appCount+1);
    //计算行号列号

    for (int index=0; index<self.apps.count; index++) {
        int col =index % appCount;
        int row =index / appCount;

        //设置一个小uiview
        UIView *appView=[[UIView alloc]init];
        //设置大小位置
        CGFloat appx=margin + col*(margin+appW);
        CGFloat appy=margin + row*(margin+appH);
        appView.frame=CGRectMake(appx, appy, appW, appH);
//        appView.backgroundColor=[UIColor blueColor];

        //添加到view
        [self.view addSubview:appView];
        //得到图片信息
        NSString *path=[[NSBundle mainBundle]pathForResource:@"app.plist" ofType:nil];
        NSArray *imageDate=[NSArray arrayWithContentsOfFile:path];
        NSDictionary *dicImage =imageDate[index];
        //在view中添加小控件
        // 添加图片
        UIImageView *imageView=[[UIImageView alloc]init];

        imageView.image=[UIImage imageNamed:dicImage[@"icon"]];
        //设置imageview 的大小

        CGFloat imageW=65;
        CGFloat imageH=65;
        CGFloat iamgex=(appView.frame.size.width-imageW)/2;
//        CGFloat iamgey=(appView.frame.size.height-imageH)/2;

        imageView.frame=CGRectMake(iamgex, 5, imageW, imageH);
        [appView addSubview:imageView];
        //添加lable 控件来显示文字
        UILabel *text=[[UILabel alloc]init];
        CGFloat texty=imageH+5;

        text.frame=CGRectMake(0, texty, appW, 30);
        text.text=dicImage[@"name"];
        text.font=[UIFont systemFontOfSize:15];
        text.textAlignment=NSTextAlignmentCenter;
//        text.backgroundColor=[UIColor greenColor];
        [appView addSubview:text];
        //添加按钮控件
        UIButton *xzbut=[UIButton buttonWithType:UIButtonTypeCustom];
        CGFloat butH=20;
        CGFloat butW=65;
        CGFloat butx=(appW-65)/2;
        CGFloat buty=100;
        xzbut.frame=CGRectMake(butx, buty, butW, butH);
        UIImage *iamgenor=[UIImage imageNamed:@"buttongreen"];
        [xzbut setBackgroundImage:iamgenor forState:UIControlStateNormal];
        iamgenor=[UIImage imageNamed:@"buttongreen_highlighted"];
        [xzbut setBackgroundImage:iamgenor forState:UIControlStateHighlighted];
        xzbut.backgroundColor=[UIColor grayColor];
        [xzbut setTitle:@"下载" forState:UIControlStateNormal];
        [xzbut setTitle:@"安装" forState:UIControlStateHighlighted];
        [appView addSubview:xzbut];




    }

效果图
在这个例子中需要掌握的知识点有

  • 通过plist文件来对内容的加载,从而减少对代码的修改
    在plist中需要知道文件的加载,文件是从相当于手机的项目文件夹中加载
    首先就得知道plist文件的路径
    NSBdle类
    NSString *path=[[NSBundle mainBundle]pathForResource:@”app.plist” ofType:nil];

  • 知道frame属性的x,y 是参考的是它的父控件

  • 在UIButton 中的setTitle:@”下载” forState:UIControlStateNormal和setBackgroundImage:iamgenor forState:UIControlStateHighlighted
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值