无法在 Swift 中以编程方式添加 NSLayoutConstraints(无效)

Can't add NSLayoutConstraints programmatically in Swift (no effect)

我在 Swift 中使用相应的 .xib 文件创建了自定义 UIView 子类,但在初始化期间添加自动布局约束时遇到问题。视图本身加载得很好,但添加布局约束似乎对视图的布局没有影响,无论我是否在 initviewDidMoveToSuperviewviewDidMoveToWindow 内添加这些约束,即使我在之后立即调用 setNeedsUpdateConstraints() / layoutIfNeeded()

我可以在 Objective-C 中很容易地做到这一点,所以希望这是一个非常简单的修复,但我不知道我做错了什么。

这是我的代码:

var isLoading = false

class CustomSubview: UIView {

    override init(frame: CGRect) {
        super.init(frame: frame)
        setup()
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        setup()
    }

    private lazy var view: UIView! = { [unowned self] in
        let bundle = NSBundle(forClass: self.dynamicType)
        let nibName = String(CustomSubview.self)
        let nib = UINib(nibName: nibName, bundle: bundle)
        return nib.instantiateWithOwner(self, options: nil)[0] as! UIView
    }()

    private func setup() {
        if (isLoading) {
            return
        }

        isLoading = true
//        self.view.frame = self.bounds // this works but I want to use Auto Layout
//        self.view.autoresizingMask = [UIViewAutoresizing.FlexibleWidth, UIViewAutoresizing.FlexibleHeight] // this works but I want to use Auto Layout
        self.translatesAutoresizingMaskIntoConstraints = false // no effect
        addSubview(self.view)
        let views = Dictionary(dictionaryLiteral: ("view", self.view))
        let horizontalConstraints = NSLayoutConstraint.constraintsWithVisualFormat("H:|[view]|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: views)
        self.addConstraints(horizontalConstraints)
        NSLayoutConstraint.activateConstraints(horizontalConstraints) // this seems to do nothing
        let verticalConstraints = NSLayoutConstraint.constraintsWithVisualFormat("V:|[view]|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: views)
        self.addConstraints(verticalConstraints)
        NSLayoutConstraint.activateConstraints(verticalConstraints) // this seems to do nothing
        self.setNeedsUpdateConstraints()
        self.layoutIfNeeded()
        isLoading = false
    }

}

我对您的 class 做了一些修改,使它看起来更干净一些。我省略了加载 Bool。此外,我没有从 XIB 文件创建视图的经验,我几乎没有动过它。

var isLoading = false

class CustomSubview: UIView {

    override init(frame: CGRect) {
        super.init(frame: frame)

        setup()
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)

        setup()
    }

    private lazy var customView: UIView = {
        let bundle = NSBundle(forClass: self.dynamicType)
        let nibName = String(CustomSubview.self)
        let nib = UINib(nibName: nibName, bundle: bundle)
        let customView = nib.instantiateWithOwner(self, options: nil)[0] as! UIView
        customView.translatesAutoresizingMaskIntoConstraints = false

        return customView
    }()

    private func setup() {
        if (isLoading) {
            return
        }

        isLoading = true
        addSubview(self.customView)
        let views = ["customView": customView]

        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|[customView]|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: views))
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|[customView]|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: views))
        isLoading = false
    }
}

至于你的纵横比约束,你可以为你的纵横比设置多个约束,并将其中的 active 设置为 true / false 以在它们之间切换。