Xcode 显示冲突的约束
Posted
技术标签:
【中文标题】Xcode 显示冲突的约束【英文标题】:Xcode shows conflicting constraints 【发布时间】:2019-04-11 12:03:28 【问题描述】:我有一个主要由 2 个视图组成的视图控制器。 一个具有与超级视图对齐的前导、尾随和底部锚点以及与超级视图(0.25)成比例的高度,以及将前导顶部和尾随与超级视图/安全区域对齐并在另一个视图底部对齐的滚动视图。
我在 xib 文件中定义了一个视图,我使用 Bundle.main.loadNibNamed("VariantResultSlide", owner: self, options: nil)?.first
创建了多次,并将它们添加到数组 slides
中。我想将它们添加到我的 ScrollView:
for i in 0 ..< slides.count
scrollView.addSubview(slides[i])
NSLayoutConstraint.activate([
slides[i].leadingAnchor.constraint(equalTo: (i==0) ? scrollView.leadingAnchor : slides[i-1].trailingAnchor),
slides[i].topAnchor.constraint(equalTo: scrollView.topAnchor),
slides[i].bottomAnchor.constraint(equalTo: scrollView.bottomAnchor),
slides[i].widthAnchor.constraint(equalTo: scrollView.widthAnchor),
slides[i].heightAnchor.constraint(equalTo: scrollView.heightAnchor)
])
if(i==slides.count-1)
NSLayoutConstraint.activate([slides[i].trailingAnchor.constraint(equalTo: scrollView.trailingAnchor)])
self.updateResultSlides(index: i, vehicle: orderedVehiclesList[i])
但是 Xcode 给了我这样的错误:
2019-04-11 13:57:07.219263+0200 FleetView[545:190663] [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:0x282cecbe0 h=-&- v=-&- FleetView.VariantResultSlide:0x107f214a0.height == UIScrollView:0x102919200.height + 99 (active)>",
"<NSLayoutConstraint:0x282c92300 FleetView.VariantResultSlide:0x107f214a0.height == UIScrollView:0x102919200.height (active)>"
)
而且幻灯片太大了。 但是我找不到为他们设置另一个约束的地方。为什么会这样?
【问题讨论】:
你试过了吗:slides[i].translatesAutoresizingMaskIntoConstraints = false
?
是的,见下文,ty
【参考方案1】:
您的视图实现了top
、bottom
和height
约束,因此很明显它的父视图需要调整大小 - 但由于自动调整掩码自动创建的约束,它不能。在你的超级视图中禁用它
yourViewsSuperview.translatesAutoresizingMaskIntoConstraints = false
【讨论】:
好的,这样做是为了幻灯片本身而不是超级视图以上是关于Xcode 显示冲突的约束的主要内容,如果未能解决你的问题,请参考以下文章
Xcode 6 或 7 面板“尺寸检查器”的约束框背景中显示的“Ag”是啥?
Xcode 9 重置大小类“查看为”以显示 Any Any 的约束
堆栈视图中的自动约束冲突、Swift 2、iOS 9.3、XCode 7