swift3.0_Constraints 的应用

func setViewConstraints() {

        //系统默认会给autoresizing 约束,要关闭autoresizing才能让自定义的约束生效,否则程序崩溃

        customView.translatesAutoresizingMaskIntoConstraints = false

        //添加约束:"哪个控件" 什么属性“ "等于/大于/小于" “另一个控件什么属性乘以 "多少" 加上 "多少"

        let Constraint_centerX = NSLayoutConstraint(item: customView, attribute: .centerX, relatedBy: .equal, toItem: otherView, attribute: .centerX, multiplier: 1, constant: 0)

        

        let Constraint_centerY = NSLayoutConstraint(item: customView, attribute: .centerY, relatedBy: .equal, toItem: otherView, attribute: .centerY, multiplier: 1, constant: 0)

        

        let Constraint_width = NSLayoutConstraint(item: customView, attribute: .width, relatedBy: .lessThanOrEqual, toItem: otherView, attribute: .width, multiplier: 1, constant: 20)

        

        let Constraint_height = NSLayoutConstraint(item: customView, attribute: .height, relatedBy: .lessThanOrEqual, toItem: otherView, attribute: .height, multiplier: 1, constant: 20)


 //激活约束

      NSLayoutConstraint.activate([Constraint_centerX,Constraint_centerY,Constraint_width,Constraint_height])

    }


你可能感兴趣的:(iOS,开发笔记)