使用自动布局使UIView全屏
问题描述:
我开始使用自动布局,并且我的UIViewController的self.view不再占用iPhone 6或iPhone 6plus的整个屏幕。 UIView的大小是size =推断的。使用自动布局使UIView全屏
我已经查看了我的VC的主视图的约束条件,并且约束编辑器不让我选择我想要的距离为0,0,0,0的顶部,左侧,右侧和底部窗口边缘的视图。
答
我做了一个函数来添加一个白色面具下一个UIView(如果你是子类的UIView)
func addBackgroundMask(){
backgroundMask = UIView()
backgroundMask?.backgroundColor = UIColor(red: 1.0, green: 1.0, blue: 1.0, alpha: 0.6)
backgroundMask?.setTranslatesAutoresizingMaskIntoConstraints(false)
let topConstraint = NSLayoutConstraint(item: backgroundMask!, attribute:NSLayoutAttribute.Top, relatedBy:NSLayoutRelation.Equal, toItem:superview, attribute:NSLayoutAttribute.Top, multiplier:1.0, constant:0.0)
let bottomConstraint = NSLayoutConstraint(item: backgroundMask!, attribute:NSLayoutAttribute.Bottom, relatedBy:NSLayoutRelation.Equal, toItem:superview, attribute:NSLayoutAttribute.Bottom, multiplier:1.0, constant:0.0)
let leftConstraint = NSLayoutConstraint(item: backgroundMask!, attribute:NSLayoutAttribute.Left, relatedBy:NSLayoutRelation.Equal, toItem:superview, attribute:NSLayoutAttribute.Left, multiplier:1.0, constant:0.0)
let rightConstraint = NSLayoutConstraint(item: backgroundMask!, attribute:NSLayoutAttribute.Right, relatedBy:NSLayoutRelation.Equal, toItem:superview, attribute:NSLayoutAttribute.Right, multiplier:1.0, constant:0.0)
superview?.insertSubview(backgroundMask!, belowSubview: self)
superview?.addConstraint(topConstraint)
superview?.addConstraint(bottomConstraint)
superview?.addConstraint(leftConstraint)
superview?.addConstraint(rightConstraint)
}