zoukankan      html  css  js  c++  java
  • ios中布局(推荐一)

    - (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");
    }
  • 相关阅读:
    个人作业3-(Alpha阶段)
    结队编程二---单元测试
    个人作业2-英语学习APP案例分析
    结队编程一
    个人作业1——四则运算题目生成程序
    软件工程的实践项目课程的自我目标
    个人附加作业
    个人作业3——个人总结(Alpha阶段)
    结对作业2:单元测试
    个人作业2——英语学习APP案例分析
  • 原文地址:https://www.cnblogs.com/gcb999/p/3273049.html
Copyright © 2011-2022 走看看