Ios Can';t在Swift中以编程方式添加NSLayoutConstraints(无效)

Ios Can';t在Swift中以编程方式添加NSLayoutConstraints(无效),ios,swift,autolayout,nslayoutconstraint,Ios,Swift,Autolayout,Nslayoutconstraint,我在Swift中创建了一个自定义UIView子类,其中包含相应的.xib文件,但在初始化过程中添加自动布局约束时遇到问题。视图本身加载得很好,但是添加布局约束似乎对视图的布局没有影响,不管我是在init还是viewdimovetowsuperview还是viewdimovetowindow中添加这些约束,甚至调用setNeedsUpdateConstraints()layoutifneed()紧接着 在Objective-C中,我可以很容易地做到这一点,所以希望这是一个非常简单的修复,但我无法找

我在Swift中创建了一个自定义UIView子类,其中包含相应的
.xib
文件,但在初始化过程中添加自动布局约束时遇到问题。视图本身加载得很好,但是添加布局约束似乎对视图的布局没有影响,不管我是在
init
还是
viewdimovetowsuperview
还是
viewdimovetowindow
中添加这些约束,甚至调用
setNeedsUpdateConstraints()
layoutifneed()
紧接着

在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
    }

}

我把你的课改了一点,让它看起来更干净一点。我省略了加载
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
    }
}

至于纵横比约束,您可以为纵横比设置多个约束,并将这些约束中的
活动
设置为
/
以在它们之间切换。

我对您的类做了一些修改,使其看起来更干净。我省略了加载
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
    }
}

至于纵横比约束,您可以为纵横比设置多个约束,并将这些约束的
活动
设置为
/
以在它们之间切换。

您是否将
视图的
translatesAutoReshizingMackintoConstraints
设置为假,这似乎也没有任何效果。(现在添加到上面)'addSubview(self.view)'对我来说很奇怪。Hi@Anupam:'self.view'实际上是一个延迟实例化的私有变量,用于从nib实例化视图。您必须设置
视图的
translatesAutoResizengMaskintoConstraints
(就像Anupam说的,我不认为把你的视图命名为false是好的。不是
UIView
本身。你把你的
视图的
TranslatesAutoReshizingMaskintoConstraints
设置为false了吗?是的,这似乎也没有任何效果。(现在添加到上面)添加子视图(self.view)'对我来说很奇怪。Hi@Anupam:'self.view'实际上是一个惰性实例化的私有变量,用于从nib实例化视图。您必须设置
视图的
translatesAutoresizengMackintoConstraints
(正如Anupam所说,我认为命名视图
视图
不是一件好事)设置为false。不是
ui视图本身。我添加了
isLoading
作为
nib返回。实例化所有者(\uu,选项:)
调用
init?(编码器:)
,导致无限循环。我添加了
isLoading
作为
nib返回。实例化所有者(\uu,选项:)
调用
init?(编码器:)
,导致无限循环。