代码改变世界

ios中布局(推荐一)

2013-08-21 17:00  甘超波  阅读(1155)  评论(0编辑  收藏  举报
- (void)viewDidLoad
{
    [super viewDidLoad];
    NSArray *data=@[@"标题一",@"标题二",@"标题三",@"标题四"];
    
    CGRect leftRect,rowRect;
    CGRectDivide(self.view.bounds, &rowRect, &leftRect, 80, CGRectMinYEdge);
    UIView *redView=[[UIView alloc] initWithFrame:rowRect];
    redView.backgroundColor=[UIColor redColor];
    [self.view addSubview:redView];
    [redView release];
    
    CGSize size=CGSizeMake(50, 50);//图片的大小

    CGFloat width=rowRect.size.width/data.count;//按钮的宽度和高度
    CGFloat heigh=rowRect.size.height;
    
    CGFloat imagew=(width-size.width)*0.5f;
    CGFloat imageh=(heigh-size.height)*0.5f;
    for (int i=0; i<data.count; i+=1) {
        

    CGRect btnRect=CGRectMake(i*width, 0, width, heigh);


        leftRect=UIEdgeInsetsInsetRect(btnRect, UIEdgeInsetsMake(imageh-10,imagew, imageh+10, imagew));//设置图片剧中


        UIImageView *imgview=[[UIImageViewalloc] initWithFrame:leftRect];


        imgview.image=[UIImage imageNamed:@"icon_weibo"];


        


        


        


        leftRect=UIEdgeInsetsInsetRect(btnRect, UIEdgeInsetsMake(btnRect.size.height-25, imagew, 5, imagew-10));


        UILabel *lb=[[UILabelalloc] initWithFrame:leftRect];


        lb.backgroundColor=[UIColorclearColor];


        lb.text=data[i];


        [redView addSubview:lb];


        [lb release];


        


        leftRect=UIEdgeInsetsInsetRect(btnRect, UIEdgeInsetsMake(5, imagew, 5, imagew));


        UIButton *btn=[[UIButtonalloc] initWithFrame:leftRect];


        [btn addTarget:selfaction:@selector(click) forControlEvents:UIControlEventTouchUpInside];


        [redView addSubview:btn];


        
        
    }
    
    
    // Do any additional setup after loading the view, typically from a nib.
}

-(void)click{
    NSLog(@"-->click");
}