我正在努力更新/简化与 Auto Layout vs NSConstraints 相关的代码,但现在它似乎让我感到困惑。
我有一些 Swift 3 代码,它通过点击按钮在 UIView 中滑动,但是 UIView 由 NSconstraints 控制并且工作正常。-
@IBAction func rightFlyOutButton(_ sender: Any) {
if (rightFlyOutShowing) {
if UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiom.pad {
leftFlyOutLeadingConstraint.constant = 25
} else {
leftFlyOutLeadingConstraint.constant = 15
}
rightFlyOutTrailingConstraint.constant = rightFlyOut.frame.width * 0.93
UIView.animate(withDuration: 0.4, animations: {self.view.layoutIfNeeded()})
} else {
if UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiom.pad {
rightFlyOutTrailingConstraint.constant = 25
} else {
rightFlyOutTrailingConstraint.constant = 15
}
UIView.animate(withDuration: 0.4, animations: {self.view.layoutIfNeeded()})
}
rightFlyOutShowing = !rightFlyOutShowing
}
尝试使用自动布局和布局锚,我想出了这个 -
@IBAction func rightFlyOutButton(_ sender: Any) {
rightFlyOut.rightAnchor.constraint(equalTo: view.rightAnchor).isActive = true
rightFlyOut.widthAnchor.constraint(equalTo: view.widthAnchor, multiplier: 0.93).isActive = true
UIView.animate(withDuration: 0.4, animations: {self.view.layoutIfNeeded()})
}
这会返回大量错误 -
[LayoutConstraints] Unable to simultaneously satisfy constraints.
Probably at least one of the constraints in the following list is one you don't want.
Try this:
(1) look at each constraint and try to figure out which you don't expect;
(2) find the code that added the unwanted constraint or constraints and fix it.
(Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints)
(
"<NSAutoresizingMaskLayoutConstraint:0x6000000949b0 h=-&& v=-&& FasteM.UIViewX:0x7fed7140c600.width == 0.500741*UIView:0x7fed7161d760.width + 150.222 (active)>",
"<NSLayoutConstraint:0x6000000900e0 FasteM.UIViewX:0x7fed7140c600.width == 0.93*UIView:0x7fed7161d760.width (active)>",
"<NSLayoutConstraint:0x600000095a90 'UIView-Encapsulated-Layout-Width' UIView:0x7fed7161d760.width == 375 (active)>"
)
我的逻辑是我将视图的右边缘锚定到超级视图并指定实际视图的宽度(即超级视图的 93%)。
我确定这是我遗漏的一个简单错误,但我将不胜感激任何正确方向的指针。
TIA