早期的IOS设备都是固定屏幕的尺寸,所以大家写iOS应用几乎都是用frame布局,直接写死宽高和坐标。
但是iPhone6开始,iOS设备出现了多种尺寸,于是苹果推出了一种新的布局方式:`autolayout`
autolaout又名自动布局,或者是是相对布局。使用autolayout可以轻易写出目前主流的所有页面布局。
关闭autoresizing布局方式
在 iOS中,默认使用aotoresizing的布局方式来写页面,autoresizing相对来说比较简单。只做了相对于父控件的大小的伸缩。使用起来不是特别顺手。
控件的translatesAutoresizingMaskIntoConstraints为false,可以启用自动布局
NSLayoutConstraint简单了解
顾名思义,这是创建一个布局约束
NSLayoutConstraint(item: 当前的控件, attribute: 约束属性, relatedBy: 前后之间的关系, toItem: 相对的控件, attribute: 相对控件的某个属性, multiplier: 比例, constant: 常量)
比如
NSLayoutConstraint(item:myview , attribute: .left, relatedBy: .equal, toItem: self.view, attribute: .left, multiplier: 1, constant: 0)
意思就是myview左边相对于self.view的左边,距离为0
创建一个与ViewController一样大的View
let myview = UIView(); self.view.addSubview(myview) myview.translatesAutoresizingMaskIntoConstraints = false myview.backgroundColor = UIColor.systemBlue let left = NSLayoutConstraint(item:myview , attribute: .left, relatedBy: .equal, toItem: self.view, attribute: .left, multiplier: 1, constant: 0) let right = NSLayoutConstraint(item: myview , attribute: .right, relatedBy: .equal, toItem: self.view , attribute: .right, multiplier: 1, constant: 0) let top = NSLayoutConstraint(item: myview , attribute: .top, relatedBy: .equal, toItem: self.view, attribute: .top, multiplier: 1, constant: 0) let bottom = NSLayoutConstraint(item: myview , attribute: .bottom, relatedBy: .equal, toItem: self.view, attribute: .bottom, multiplier: 1, constant: 0) myview.superview?.addConstraints([left,right,top,bottom])
约束某个某个属性为固定值
比如我们需要设置当前view的宽度为100.高度为200
let myview = UIView(); self.view.addSubview(myview) myview.translatesAutoresizingMaskIntoConstraints = false myview.backgroundColor = UIColor.systemBlue let left = NSLayoutConstraint(item:myview , attribute: .left, relatedBy: .equal, toItem: self.view, attribute: .left, multiplier: 1, constant: 0) let width = NSLayoutConstraint(item: myview , attribute: .width, relatedBy: .equal, toItem: nil , attribute: .notAnAttribute, multiplier: 1, constant: 100) let top = NSLayoutConstraint(item: myview , attribute: .top, relatedBy: .equal, toItem: self.view, attribute: .top, multiplier: 1, constant: 0) let height = NSLayoutConstraint(item: myview , attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1, constant: 200) myview.superview?.addConstraints([left,width,top,height])
View居中
let myview = UIView(); self.view.addSubview(myview) myview.translatesAutoresizingMaskIntoConstraints = false myview.backgroundColor = UIColor.systemBlue let centerX = NSLayoutConstraint(item:myview , attribute: .centerX, relatedBy: .equal, toItem: self.view, attribute: .centerX, multiplier: 1, constant: 0) let width = NSLayoutConstraint(item: myview , attribute: .width, relatedBy: .equal, toItem: nil , attribute: .notAnAttribute, multiplier: 1, constant: 100) let centerY = NSLayoutConstraint(item: myview , attribute: .centerY, relatedBy: .equal, toItem: self.view, attribute: .centerY, multiplier: 1, constant: 0) let height = NSLayoutConstraint(item: myview , attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1, constant: 200) myview.superview?.addConstraints([centerX,width,centerY,height])