0

我正在努力更新/简化与 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

4

2 回答 2

1

第二种方法的问题在于,每次点击都会创建其他约束,这将与旧的相同约束发生冲突,因此您应该在任何地方创建这些约束,获取对它们的引用并像第一次那样更改它们的常量。正确的方法

于 2018-03-06T17:33:08.963 回答
0

我已经解决了这个问题,最后想出了一个可行的解决方案 -

首先创建一个变量:

var LeftFlyOutConstraint: NSLayoutConstraint?

然后在 viewDidLoad() 中使用以下代码:

leftFlyOut.translatesAutoresizingMaskIntoConstraints = false    
LeftFlyOutConstraint = leftFlyOut.trailingAnchor.constraint(equalTo: view.leadingAnchor, constant: 35)
    LeftFlyOutConstraint?.isActive = true

最后是按钮:

@IBAction func leftFlyOutButton(_ sender: Any) {
        if (leftFlyOutShowing) {
            LeftFlyOutConstraint?.isActive = false
                LeftFlyOutConstraint = leftFlyOut.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -40)
            LeftFlyOutConstraint?.isActive = true
            UIView.animate(withDuration: 0.4, animations: {self.view.layoutIfNeeded()})
        } else {
            LeftFlyOutConstraint?.isActive = false
            LeftFlyOutConstraint = leftFlyOut.trailingAnchor.constraint(equalTo: view.leadingAnchor, constant: 35)
            LeftFlyOutConstraint?.isActive = true
            UIView.animate(withDuration: 0.4, animations: {self.view.layoutIfNeeded()})
        }
        leftFlyOutShowing = !leftFlyOutShowing
    }

并且没有报告错误。

于 2018-03-07T08:32:12.040 回答