使用autolayout使UIView全屏显示

时间:2015-02-05 15:23:13

标签: uiview autolayout

我开始使用autolayout而我的UIViewController的self.view不再占用iPhone 6或iPhone 6plus的整个屏幕。 UIView的大小是size =推断。

我已经查看了我的VC主视图的约束条件,并且约束编辑器不允许我选择我希望顶部,左侧,右侧和底部的距离为0,0,0,0视窗边缘的视图。

1 个答案:

答案 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)

}