Ios 在Swift 4中布局锚固件

Ios 在Swift 4中布局锚固件,ios,swift,autolayout,layout-anchor,Ios,Swift,Autolayout,Layout Anchor,我正在努力更新/简化与自动布局和NSConstraints相关的代码,但是现在它似乎让我感到困惑 我有一些Swift 3代码,点击一个按钮就可以在UIView中滑动,但是UIView由NSconstraints控制,工作正常- @IBAction func rightFlyOutButton(_ sender: Any) { if (rightFlyOutShowing) { if UI_USER_INTERFACE_IDIOM() == UIUserIn

我正在努力更新/简化与自动布局和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)>"
)
[LayoutConstraints]无法同时满足约束。
可能下面列表中至少有一个约束是您不想要的。
试试这个:
(1) 看看每一个约束,试着找出你不期望的;
(2) 找到添加了不需要的约束的代码,然后修复它。
(注意:如果您看到不理解的NSAutoresizingMaskLayoutConstraints,请参阅UIView属性TranslatesAutoResizingMaskToConstraints的文档)
(
"",
"",
""
)
我的逻辑是将视图的右边缘锚定到superview,并指定实际视图的宽度(即superview的93%)

我确信这是一个我没有注意到的简单错误,但我希望能有任何指向正确方向的指针


TIA第二种方法的问题是,每次单击都会创建其他约束,这些约束将与旧的相同约束冲突,因此您应该在任何地方创建这些约束,获取对它们的引用并更改它们的常量,就像您在第一种右方法中所做的那样纳利想出了一个有效的解决方案-

首先创建一个变量:

var LeftFlyOutConstraint: NSLayoutConstraint?
然后在viewDidLoad()中使用以下代码:

最后是按钮:

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

而且没有报告错误。

这一回答似乎切中要害。它被无缘无故地否决了,真是太遗憾了。@Shu Khan,谢谢你的回答。你是建议我继续使用NSConstraints版本(Swift 3)还是只建议我在更改常量(Swift 4)之前指定原始锚定?是的,在更改按钮操作中的常数之前,指定原始锚定并通过声明NSLayoutConstraint VAR引用它们,请看这里添加到Sh_Khan的答案中,这是一个很好的建议,看起来您正在制作动画的视图上也有自动调整大小的遮罩。通常使用自动布局时,您不使用自动调整大小的遮罩,(思考
翻译自动调整大小GMaskintoConstraints=false
)。如果您想要自动调整大小的遮罩,请忽略此注释,但如果您不想要或不知道它们是什么,我建议您将其关闭。它可能无法完全按照您的意愿修复动画,但它将消除控制台中一些您不理解的奇怪约束。
@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
    }