查看约束 - 适合子视图

时间:2014-09-06 15:27:07

标签: ios uiview swift autolayout

我正在为视图添加子视图,我希望它填充视图的高度和宽度。我对约束有困难。任何帮助表示赞赏。这就是我目前所拥有的:

    self.view.addSubview(self.mainView)

    var leftSideConstraint = NSLayoutConstraint(item: self.mainView, attribute: .Left, relatedBy: .Equal, toItem: self.view, attribute: .Left, multiplier: 1.0, constant: 0.0)
    var bottomConstraint = NSLayoutConstraint(item: self.mainView, attribute: .Bottom, relatedBy: .Equal, toItem: self.view, attribute: .Bottom, multiplier: 1.0, constant: 0.0)
    var widthConstraint = NSLayoutConstraint(item: self.mainView, attribute: .Width, relatedBy: .Equal, toItem: self.view, attribute: .Width, multiplier: 1.0, constant: 0.0)
    var heightConstraint = NSLayoutConstraint(item: self.mainView, attribute: .Height, relatedBy: .Equal, toItem: self.view, attribute: .Height, multiplier: 1.0, constant: 0.0)
    self.view.addConstraints([leftSideConstraint, bottomConstraint, widthConstraint, heightConstraint])

2 个答案:

答案 0 :(得分:7)

Swift 4,iOS 11

这是使用UIView扩展

的优雅方式
extension UIView {
    func addConstrained(subview: UIView) {
        addSubview(subview)
        subview.translatesAutoresizingMaskIntoConstraints = false
        subview.topAnchor.constraint(equalTo: topAnchor).isActive = true
        subview.leadingAnchor.constraint(equalTo: leadingAnchor).isActive = true
        subview.trailingAnchor.constraint(equalTo: trailingAnchor).isActive = true
        subview.bottomAnchor.constraint(equalTo: bottomAnchor).isActive = true
    }
}

然后使用

在任何需要的地方添加子视图
yourContainerView.addConstrained(subview: yourSubview)

答案 1 :(得分:5)

以下代码应该有效:

    let mainView = UIView() //Make sure your mainView is initialized
    mainView.backgroundColor = UIColor.blueColor() //For test purpose

    mainView.setTranslatesAutoresizingMaskIntoConstraints(false) //Don't forget this line
    view.addSubview(mainView)

    var leftSideConstraint = NSLayoutConstraint(item: mainView, attribute: .Left, relatedBy: .Equal, toItem: view, attribute: .Left, multiplier: 1.0, constant: 0.0)
    var bottomConstraint = NSLayoutConstraint(item: mainView, attribute: .Bottom, relatedBy: .Equal, toItem: view, attribute: .Bottom, multiplier: 1.0, constant: 0.0)
    var widthConstraint = NSLayoutConstraint(item: mainView, attribute: .Width, relatedBy: .Equal, toItem: view, attribute: .Width, multiplier: 1.0, constant: 0.0)
    var heightConstraint = NSLayoutConstraint(item: mainView, attribute: .Height, relatedBy: .Equal, toItem: view, attribute: .Height, multiplier: 1.0, constant: 0.0)
    view.addConstraints([leftSideConstraint, bottomConstraint, heightConstraint, widthConstraint])

作为替代方案,您可以使用自动布局可视格式语言:

    let mainView = UIView() //Make sure your mainView is initialized
    mainView.backgroundColor = UIColor.blueColor() //For test purpose

    mainView.setTranslatesAutoresizingMaskIntoConstraints(false) //Don't forget this line
    view.addSubview(mainView)

    var viewsDict = ["mainView" : mainView]

    var horizontalConstraints = NSLayoutConstraint.constraintsWithVisualFormat("H:|[mainView]|", options: NSLayoutFormatOptions(0), metrics: nil, views: viewsDict)
    var verticalConstraints = NSLayoutConstraint.constraintsWithVisualFormat("V:|[mainView]|", options: NSLayoutFormatOptions(0), metrics: nil, views: viewsDict)
    view.addConstraints(horizontalConstraints)
    view.addConstraints(verticalConstraints)