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