我注意到,当我以编程方式更新自动布局约束时,当我旋转屏幕时,所有更改都会恢复。
重现问题:
对我来说似乎是一个错误。
你怎么认为 ?
截图:
Storyboard:
约束#1:
约束#2:
尺码等级
已安装指的是尺寸等级安装,而不是指active/inactive.
您必须以编程方式创建另一个约束,并激活/停用that一。这是因为您无法更改约束的乘数 (我可以更改 NSLayoutConstraint 的 multiplier 属性吗?),你也不能修改尺寸类别(activateConstraints:和 deactivateConstraints:在 IB 中创建的约束旋转后不保留).
有几种方法可以做到这一点。在下面的示例中,我创建了您的副本x1约束,用乘数或1/2。然后我在两者之间切换:
@IBOutlet var fullWidthConstraint: NSLayoutConstraint!
var halfWidthConstraint: NSLayoutConstraint!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
halfWidthConstraint = NSLayoutConstraint(item: fullWidthConstraint.firstItem,
attribute: fullWidthConstraint.firstAttribute,
relatedBy: fullWidthConstraint.relation,
toItem: fullWidthConstraint.secondItem,
attribute: fullWidthConstraint.secondAttribute,
multiplier: 0.5,
constant: fullWidthConstraint.constant)
halfWidthConstraint.priority = fullWidthConstraint.priority
}
@IBAction func changeConstraintAction(sender: UISwitch) {
if sender.on {
NSLayoutConstraint.deactivateConstraints([fullWidthConstraint])
NSLayoutConstraint.activateConstraints([halfWidthConstraint])
} else {
NSLayoutConstraint.deactivateConstraints([halfWidthConstraint])
NSLayoutConstraint.activateConstraints([fullWidthConstraint])
}
}
测试于iOS 9+, Xcode 7+.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)