开发中对storyBoard的误解以及手码约束

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的使用

"|"表示superview. 
    |-间距-[view1对象名]-(>=20)-[view2对象名]
    不写H/V就表示横向,间距可以写固定值也可写>/<。
    形象化的理解,"|"是用来确定view上、下、左、右关系的。
    想要确定从上到下的关系,就加V:|。那么这个vfl字串就可以描述从上到下的view们的关系。

    //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]];





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值