1.现在有的公司在开发中可能还存在对storyboard的误解
说storyboard是XML的,性能不好,且不适合团队开发。
稍微一动就会改变storyboard的状态,在公司团队开发中造成了很大的困扰,因此很多公司拒绝使用storyboard。
其实,以上的bug是XCode4.x以前的,Xcode5.0以后,这些bug就已被修复了;
如果真的不小心改变了storyboard中控件的位置,可以使用git恢复。
还有一点,在项目中只要一编译就会把storyboard转化为二进制文件,在执行的过程中并不会很慢,所以性能上来说不会差很多
测试:我们可以显示boundle内容,找到storyboard,此时已经不能用XCode打开了,可以显示包内容,在终端输入以下命令
终端命令:cd Main.storyboardc
ls
xxd 要查看的文件.nib 可以发现其已经是一个二进制数据,打成了一个包
2.那我们在开发中怎么选择呢?
使用 xib 和 storyboard 的优点:
1.可以迅速的完成界面的搭建工作,比手码更加快捷,可以很快的看见效果,利于调试
2.对新人来说这跟容易理清各个模块间的跳转关系
纯手码的优点:
1.便于统一管理,可以很快的修改统一样式
2.“代码是王道”,可以做到storyboard做不到的事情
例如:把一个按钮加进一个UILabel中,storyboard就做不到,只能是同级关系;
但是纯手码就可以,因为UILabel继承自UIView,继承自UIView的控件都可以容纳子控件
在开发中我们可以根据公司的实际需求和个人爱好来选择。但是身为一个IOS开发者,不论是手码还是storyboard都应该熟练的使用。
3.约束
1.VFL的使用
//1 创建控件
UIView *blueView = [[UIView alloc] init];
[self.view addSubview:blueView];
blueView.backgroundColor = [UIColor blueColor];
UIView *redView = [[UIView alloc] init];
[self.view addSubview:redView];
redView.backgroundColor = [UIColor redColor];
//2 禁用Autoresizing
redView.translatesAutoresizingMaskIntoConstraints = NO;
blueView.translatesAutoresizingMaskIntoConstraints = NO;
//3 创建约束
//blueView的约束
NSArray *blueViewHCon = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-pd1-[bView]-pd2-|" options:0 metrics:@{@"pd1":@20,@"pd2":@20} views:@{@"bView":blueView}];
NSArray *blueViewVCon = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-20-[bView(50)]-20-[rView(==bView)]" options:NSLayoutFormatAlignAllTrailing metrics:nil views:@{@"bView":blueView,@"rView":redView}];
NSLayoutConstraint *redViewWidth = [NSLayoutConstraint constraintWithItem:redView attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:blueView attribute:NSLayoutAttributeWidth multiplier:0.5 constant:0];
//4 添加约束
[self.view addConstraints:blueViewHCon];
[self.view addConstraints:blueViewVCon];
[self.view addConstraint:redViewWidth];
2.手码约束
// 添加关闭按钮
UIButton *closeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
[closeBtn setBackgroundImage:[UIImage imageNamed:@"alphaClose"] forState:UIControlStateNormal];
[img addSubview:closeBtn];
// 添加约束注意点
/*
1.关闭autoreszing
2.添加到父控件中
3.创建约束
4.添加约束
*/
closeBtn.translatesAutoresizingMaskIntoConstraints = NO;
// top
NSLayoutConstraint *topC = [NSLayoutConstraint constraintWithItem:closeBtn attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:closeBtn.superview attribute:NSLayoutAttributeTop multiplier:1.0 constant:0];
// right
NSLayoutConstraint *rightC = [NSLayoutConstraint constraintWithItem:closeBtn attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:closeBtn.superview attribute:NSLayoutAttributeRight multiplier:1.0 constant:0];
// width
NSLayoutConstraint *withC = [NSLayoutConstraint constraintWithItem:closeBtn attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:20];
// height
NSLayoutConstraint *heightC = [NSLayoutConstraint constraintWithItem:closeBtn attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:20];
// 添加约束
[img addConstraints:@[topC,rightC]];
[closeBtn addConstraints:@[withC,heightC]];