ios - Swift 以编程方式设置视图约束错误日志
问题描述
我有UIViewController
几个视图,其中一个是包含广告视图(AdMob)的 UIView。
这就是我设置视图的方式Constraint
:
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
可见):
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))
问题是我收到此错误:
[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.
这是我正在使用的锚方法:
open func anchor(top: NSLayoutYAxisAnchor?, leading: NSLayoutXAxisAnchor?, bottom: NSLayoutYAxisAnchor?, trailing: NSLayoutXAxisAnchor?, padding: UIEdgeInsets = .zero, size: CGSize = .zero) -> AnchoredConstraints {
translatesAutoresizingMaskIntoConstraints = false
var anchoredConstraints = AnchoredConstraints()
if let top = top {
anchoredConstraints.top = topAnchor.constraint(equalTo: top, constant: padding.top)
}
if let leading = leading {
anchoredConstraints.leading = leadingAnchor.constraint(equalTo: leading, constant: padding.left)
}
if let bottom = bottom {
anchoredConstraints.bottom = bottomAnchor.constraint(equalTo: bottom, constant: -padding.bottom)
}
if let trailing = trailing {
anchoredConstraints.trailing = trailingAnchor.constraint(equalTo: trailing, constant: -padding.right)
}
if size.width != 0 {
anchoredConstraints.width = widthAnchor.constraint(equalToConstant: size.width)
}
if size.height != 0 {
anchoredConstraints.height = heightAnchor.constraint(equalToConstant: size.height)
}
[anchoredConstraints.top, anchoredConstraints.leading, anchoredConstraints.bottom, anchoredConstraints.trailing, anchoredConstraints.width, anchoredConstraints.height].forEach{ $0?.isActive = true }
return anchoredConstraints
}
知道有什么问题吗?
解决方案
当广告可用时,您应该在此处使用创建的底部约束
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!
并且最初在这里为底部锚提供 nil
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
在此之后,当您需要以恒定的值显示广告时
bottomCon.constant = 0
self.view.layoutIfNeeded()
推荐阅读
- iis - 在 IIS 中为网站绑定添加 SSL 证书时出错
- nextflow - 如何指示下一个输出在哪里?
- javascript - 在旧版浏览器上设置 css --variables 会破坏我的 js 脚本吗?
- python - 在域名上集群 IP 地址
- mysql - GROUP_CONCAT 和 GROUP BY 中的 ORDER BY 与连接表
- c# - Winforms 和 VSTO Excel ShowDialog() 部分模态
- r - 为什么 RMSE 值在较小的树上增加(RPART)
- javascript - 如何使用javascript获取特定周的第一天(11月的第2周)和特定周的最后一天(11月的第4周)
- redis - Kubernetes - 无法从同一集群上的另一个 pod 连接到 redis pod
- raspberry-pi - 使用树莓派 3 模型 B 的汽车电池寿命