在运行时更新锚点-AutoLayout

时间:2018-11-08 05:12:18

标签: ios swift autolayout ios-autolayout

我没有使用IBOutlets,因此给定的重复问题无法回答此问题。

我通过编程方式进行了如下布局设置。

view.addSubview(myView)
        NSLayoutConstraint.activate([
            myView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor, constant: 8),
            myView.leftAnchor.constraint(equalTo: view.leftAnchor, constant: 8),
            myView.rightAnchor.constraint(equalTo: view.rightAnchor, constant: -8),
            myView.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor, constant: -200)
            ])

现在在运行时,我想将底部锚点的常数更新为-100。但是当我这样做时,布局不会更新,并且错误消息会打印在控制台中。

self.myView.bottomAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.bottomAnchor, constant: -100).isActive = true
        self.view.layoutIfNeeded()

这是控制台中显示的错误消息的一部分。

018-11-08 10:25:14.415065+0530 UI test[19594:13754622] [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. 
(
    "<NSLayoutConstraint:0x600001653980 UIView:0x7fba334036c0.bottom == UILayoutGuide:0x600000c440e0'UIViewSafeAreaLayoutGuide'.bottom - 200   (active)>",
    "<NSLayoutConstraint:0x600001660550 UIView:0x7fba334036c0.bottom == UILayoutGuide:0x600000c440e0'UIViewSafeAreaLayoutGuide'.bottom - 100   (active)>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x600001660550 UIView:0x7fba334036c0.bottom == UILayoutGuide:0x600000c440e0'UIViewSafeAreaLayoutGuide'.bottom - 100   (active)>

Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKitCore/UIView.h> may also be helpful.

0 个答案:

没有答案