[英]Xcode shows conflicting constraints
我有一個主要由2個視圖組成的視圖控制器。 一個具有前導,尾隨和底錨與Superview和比例高度對齊的Superview(0.25),以及一個滾動視圖,將前導頂和尾隨與Superview /安全區域和底端對齊。
我在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)>"
)
幻燈片太大了。 但是我找不到任何為它們設置其他約束的地方。 為什么會這樣呢?
您的視圖實現了top
, bottom
和height
約束,因此很明顯,需要調整其top
視圖的大小-但它不會由於自動調整蒙版自動創建的約束而導致。 在您的超級視圖中將其禁用
yourViewsSuperview.translatesAutoresizingMaskIntoConstraints = false
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.