Ios Swift以编程方式设置视图约束错误日志

Ios Swift以编程方式设置视图约束错误日志,ios,objective-c,swift,xcode,constraints,Ios,Objective C,Swift,Xcode,Constraints,我有UIViewController和几个视图,其中一个是包含ads视图(AdMob)的UIView 这就是我设置视图约束的方式: selectBtn.anchor(top: view.safeAreaLayoutGuide.topAnchor, leading: nil, bottom: nil, trailing: nil, padding: .init(top: 50.0, left: 10.0, bottom: 0, right: 10.0)) selectBtn.centerXToSu

我有
UIViewController
和几个视图,其中一个是包含ads视图(AdMob)的UIView

这就是我设置视图约束的方式:

selectBtn.anchor(top: view.safeAreaLayoutGuide.topAnchor, leading: nil, bottom: nil, trailing: nil, padding: .init(top: 50.0, left: 10.0, bottom: 0, right: 10.0))
selectBtn.centerXToSuperview()
        
statusLabel.anchor(top: selectBtn.bottomAnchor, leading: view.safeAreaLayoutGuide.leadingAnchor, bottom: nil, trailing: view.safeAreaLayoutGuide.trailingAnchor, padding: .init(top: 20.0, left: 10.0, bottom: 0, right: 10.0))

fileSizeLabel.anchor(top: statusLabel.bottomAnchor, leading: view.safeAreaLayoutGuide.leadingAnchor, bottom: nil, trailing: view.safeAreaLayoutGuide.trailingAnchor, padding: .init(top: 10.0, left: 10.0, bottom: 0, right: 10.0))

adView.anchor(top: nil, leading: view.safeAreaLayoutGuide.leadingAnchor, bottom: view.safeAreaLayoutGuide.bottomAnchor, trailing: view.safeAreaLayoutGuide.trailingAnchor, padding: .init(top: 0.0, left: 0.0, bottom: -50.0, right: 0.0), size: .init(width: view.width, height: 50.0))

imageView.anchor(top: fileSizeLabel.bottomAnchor, leading: view.safeAreaLayoutGuide.leadingAnchor, bottom: adView.topAnchor, trailing: view.safeAreaLayoutGuide.trailingAnchor, padding: .init(top: 10.0, left: 10.0, bottom: 10.0, right: 10.0))
adView
是全尺寸的,但由于我添加了填充,因此在视图下(我只想在广告可用时显示
adView

当广告可用时,我调用此代码(使
adview
可见):

问题是我遇到了以下错误:

[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:0x600001c34af0 UIView:0x7fbdf040e130.bottom == UILayoutGuide:0x60000061ae60'UIViewSafeAreaLayoutGuide'.bottom + 50   (active)>",
    "<NSLayoutConstraint:0x600001c3c2d0 UIView:0x7fbdf040e130.bottom == UILayoutGuide:0x60000061ae60'UIViewSafeAreaLayoutGuide'.bottom   (active)>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x600001c34af0 UIView:0x7fbdf040e130.bottom == UILayoutGuide:0x60000061ae60'UIViewSafeAreaLayoutGuide'.bottom + 50   (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.

知道问题出在哪里吗?

当广告可用时,您应该在此处使用创建的底部约束

adView.anchor(top: nil, leading: view.safeAreaLayoutGuide.leadingAnchor, bottom: view.safeAreaLayoutGuide.bottomAnchor, trailing: view.safeAreaLayoutGuide.trailingAnchor, padding: .init(top: 0.0, left: 0.0, bottom: -50.0, right: 0.0), size: .init(width: view.width, height: 50.0))
但当您在这里创建另一个底部约束时

adView.anchor(top: nil, leading: view.safeAreaLayoutGuide.leadingAnchor, bottom: view.safeAreaLayoutGuide.bottomAnchor, trailing: view.safeAreaLayoutGuide.trailingAnchor, padding: .init(top: 0.0, left: 0.0, bottom: 0.0, right: 0.0), size: .init(width: view.width, height: 50.0))
它与旧的1冲突,因此您应该有一个可以使用的实例

var bottomCon:NSLayoutConstraint!
并在此处为底部锚提供零初始值

adView.anchor(top: nil, leading: view.safeAreaLayoutGuide.leadingAnchor, bottom:nil, trailing: view.safeAreaLayoutGuide.trailingAnchor, padding: .init(top: 0.0, left: 0.0, bottom: -50.0, right: 0.0), size: .init(width: view.width, height: 50.0))
加上像这样的

bottomCon = adView.bottomAnchor.constraint(equalTo:view.safeAreaLayoutGuide.bottomAnchor,constant:50)
bottomCon.isActive = true
在这之后,当你需要用恒定的valie显示广告播放时

bottomCon.constant = 0
self.view.layoutIfNeeded()

您为
adView
提供了冲突的约束

adView.anchor(top: nil, leading: view.safeAreaLayoutGuide.leadingAnchor, bottom: view.safeAreaLayoutGuide.bottomAnchor, trailing: view.safeAreaLayoutGuide.trailingAnchor, padding: .init(top: 0.0, left: 0.0, bottom: -50.0, right: 0.0), size: .init(width: view.width, height: 50.0))
提供底部构造和衬垫。删除其中一个,它将正常工作

bottomCon.constant = 0
self.view.layoutIfNeeded()
adView.anchor(top: nil, leading: view.safeAreaLayoutGuide.leadingAnchor, bottom: view.safeAreaLayoutGuide.bottomAnchor, trailing: view.safeAreaLayoutGuide.trailingAnchor, padding: .init(top: 0.0, left: 0.0, bottom: -50.0, right: 0.0), size: .init(width: view.width, height: 50.0))