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

标签 ios swift autolayout nslayoutconstraint

我在 Swift 中使用相应的 .xib 文件创建了一个自定义 UIView 子类,但在初始化期间添加自动布局约束时遇到了问题。 View 本身加载得很好,但添加布局约束似乎对 View 的布局没有影响,无论我是在 initviewDidMoveToSuperview 中添加这些约束viewDidMoveToWindow,即使我在之后立即调用 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 文件创建 View 的经验,我几乎没有动过它。

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在它们之间切换。

关于ios - 无法在 Swift 中以编程方式添加 NSLayoutConstraints(无效),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34997476/

相关文章:

ios - 如何判断 MagicalRecord 操作是否成功?

ios - 单击 Shinobi 图表时如何创建和显示标签以显示 X/Y 值?

swift - UIWebView 未显示(Swift)

ios - 使用 swift 后台获取本地通知

ios - 根据 `UIView` `UILabel` 中的文本更改 `UITableViewCell` 宽度

ios - 隐藏包含 UITextview 的 UITableviewcell(自动布局)

ios - 自动布局 Xcode 6

ios - 在 native iOS设置中更改位置权限时收到通知

ios - Xcode应用程序键盘看起来很拉长

ios - 实例方法 'widgetPerformUpdate(completionHandler:)' 几乎匹配可选要求 'widgetPerformUpdate(completionHandler:)'