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
ios-九宫格布局
最新推荐文章于 2018-10-25 16:32:45 发布