代码约束与VFL

一、苹果原生自动布局介绍

二、代码约束(code constraint)

1、操作步骤

一、首先先设置子控件的属性,如懒加载控件,再将控件添加到父视图上。
二、取消所有子控件的translatersAutoresizingMaskIntoConstraints(自动适配autoresizing)设置成false.
//代码约束时这个要设置成false
        for v in subviews{
            v.translatesAutoresizingMaskIntoConstraints = false
        }
三、添加约束
//设置x,y,因为是图片,可以只设置x,y
        addConstraint(NSLayoutConstraint(item: backImageView,
                                         attribute: .centerX,
                                         relatedBy: .equal,
                                         toItem: self,
                                         attribute: .centerX,
                                         multiplier: 1.0,
                                         constant: 0))
注意:当是按钮或UIImageView设置好图片好,只用设置它们的x、y的值就可

 2、使用规则

 addConstraint(NSLayoutConstraint(item: backImageView,
                                         attribute: .centerX,
                                         relatedBy: .equal,
                                         toItem: self,
                                         attribute: .centerX,
                                         multiplier: 1.0,
                                         constant: 0))
 第1个item:视图
   attribute:约束属性,如左边,右边,尾部,居中,顶部(.left,.right,.bottom,.top)
  relatedBy:约束关系,如大于,等于,小于等,.equal
      toItem:参照视图,如父视图 self,如果参照视图是自己的话设置为nil
    attribute:参照视图约束属性,如果参照视图是自己的话设置设置为.notAnAttribute
   multiplier: 乘积,如1.0,0.5
    constant:约束数值

分析: 上面的例子,添加backImageView,它的中心点x等于父视图的中心点x乘以1再加上0

 三、VFL

1、操作步骤

  1.1使用方法

  

  1.2操作函数addConstraints(NSLayoutConstraint.constraints(withVisualFormat: <#T##String#>, options: <#T##NSLayoutFormatOptions#>, metrics: <#T##[String : Any]?#>, views: <#T##[String : Any]#>))

2、使用规则

 

posted @ 2017-03-02 09:13  TheYouth  阅读(316)  评论(0编辑  收藏  举报