ios-九宫格布局

88 篇文章 0 订阅
UI 布局很重要 现在iOS学习的同学很多 
为了帮助大家尽快的入门 博客中会穿插一些简单的例子  
虽然例子简单 但是原理很重要 
工作中我们是一个工程师 而不是一个码农 
做代码的搬运工 
在工作中 很多人会感觉很累 因为自己每天都在写一些重复的代码 
这里垃圾代码给自己造成了很大的麻烦  加班加点 
使项目维护起来很困难 自己跳槽之后 给后面的新同事留下的也是坑 
所有我们的代码要向一些大神靠近 大神写的代码简单易懂 
今天给出个UI 最基本的布局 九宫格  
要学会如何减少写垃圾代码 这样自己的技术才会有进步 
直接上代码 :
#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    int num = 9;
    
    int totals = 3;
    
    CGFloat demoW = 80;
    CGFloat demoH = 80;
    CGFloat margin = (self.view.frame.size.width - demoW*totals)/(totals+1);
    //CGFloat demoX = margin;
    //CGFloat demoY = margin;
    
    for (int i = 0 ; i < num; i++) {
        
        int row = i / totals;
        
        int colums = i % totals;
        
        UIView * demo = [[UIView alloc]init];
        
    
        demo.frame = CGRectMake(margin + (margin + demoW)*colums, margin + (margin + demoH)*row, demoW, demoH);
        demo.layer.cornerRadius = 40;
        
        UIImageView *imageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"5"]];
        demo.clipsToBounds = YES;
        [demo addSubview:imageView];
        demo.backgroundColor = [UIColor redColor];
        
        [self.view addSubview:demo];

    }
    
    
    
//    UIView * demo = [[UIView alloc]init];
//    
//    demo.frame = CGRectMake(demoX, demoY, demoW, demoH);
//    
//    demo.backgroundColor = [UIColor redColor];
//    
//    UIView *demo2 = [[UIView alloc]init];
//    demo2.frame = CGRectMake(margin + (margin + demoW), 30, demoW, demoH);
//    demo2.backgroundColor = [UIColor redColor];
//    
//    
//    UIView *demo3 = [[UIView alloc]init];
//    demo3.frame = CGRectMake(margin + (demoW + margin)*2, 30, demoW, demoH);
//    demo3.backgroundColor = [UIColor redColor];
//    
//    
//    UIView *demo4 = [[UIView alloc]init];
//    demo4.frame = CGRectMake(margin, margin + (demoH + margin), demoW, demoH);
//    demo4.backgroundColor = [UIColor redColor];
//    
//    
//    [self.view addSubview:demo4];
//    [self.view addSubview:demo3];
//    [self.view addSubview:demo2];
//    [self.view addSubview:demo];
    
    
    
    // Do any additional setup after loading the view, typically from a nib.
}


- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
   
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值