iOS |以编程方式添加两个视图时出现布局约束错误
iOS | Layout constraint error when adding two views programatically
我有一段代码可以根据提供的约束按预期显示图像视图和 UILabel side.It,但控制台中有很多警告
这里是错误
[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:0x6000003d0190 V:|-(0)-[UIImageView:0x7faa7401b940] (active, names: '|':Movies_TODO.MovieViewCell:0x7faa7402caa0'custom' )>",
"<NSLayoutConstraint:0x6000003d05a0 UIImageView:0x7faa7401b940.bottom == Movies_TODO.MovieViewCell:0x7faa7402caa0'custom'.bottom (active)>",
"<NSLayoutConstraint:0x6000003d0640 UIImageView:0x7faa7401b940.height == 100 (active)>",
"<NSLayoutConstraint:0x6000003e7070 'UIView-Encapsulated-Layout-Height' Movies_TODO.MovieViewCell:0x7faa7402caa0'custom'.height == 100.5 (active)>"
)
将尝试通过打破约束来恢复
这是我的一段代码
self.addSubview(movieImageView)
self.addSubview(titleLabel)
movieImageView.topAnchor.constraint(equalTo: self.topAnchor).isActive = true
movieImageView.leadingAnchor.constraint(equalTo: self.leadingAnchor).isActive = true
movieImageView.bottomAnchor.constraint(equalTo: self.bottomAnchor).isActive = true
movieImageView.widthAnchor.constraint(equalToConstant: 100).isActive = true
movieImageView.heightAnchor.constraint(equalToConstant: 100).isActive = true
titleLabel.leadingAnchor.constraint(equalTo: movieImageView.trailingAnchor, constant: 20).isActive = true
titleLabel.topAnchor.constraint(equalTo: self.topAnchor).isActive = true
titleLabel.trailingAnchor.constraint(equalTo: self.trailingAnchor).isActive = true
titleLabel.bottomAnchor.constraint(equalTo: self.bottomAnchor).isActive = true
您的 movieImageView
具有类似 height equal to 100
和 height equal to super view's height
的约束(这是因为您添加了顶部和底部锚点约束)。
1 - 如果你想从上到下设置 movieImageView
只需删除 movieImageView.heightAnchor.constraint(equalToConstant: 100).isActive = true
.
2 - 如果你想将 movieImageView
的高度设置为 100,只需删除你不想为 movieImageView
->
设置的行
// if you want to stick your imageView to bottom delete this
movieImageView.topAnchor.constraint(equalTo: self.topAnchor).isActive = true
// if you want to stick your imageView to top delete this
movieImageView.bottomAnchor.constraint(equalTo: self.bottomAnchor).isActive = true
和UIStackView
- 如果你想创建一个
TableViewCell
有左侧图像和右侧标签,你可以使用 StackView
并且你可以不受任何限制地做你想做的事。
创建一个UIStackView
let stackView = UIStackView()
添加你的movieImageView
和titleLabel
stackView.addArrangedSubviews(movieImageView, titleLabel)
stackView.axis = .horizontal
stackView.spacing = 20.0
将您的 stackView
添加到 super.view
self.addSubview(stackView)
stackView.translatesAutoresizingMaskIntoConstraints = false
stackView.topAnchor.constraint(equalTo: self.topAnchor).isActive = true
stackView.bottomAnchor.constraint(equalTo: self.bottomAnchor).isActive = true
stackView. trailingAnchor.constraint(equalTo: self. trailingAnchor).isActive = true
stackView.leadingAnchor.constraint(equalTo: self.leadingAnchor).isActive = true
如果你想根据它的图像设置movieImageView
的宽度,你应该设置movieImageView
[=40的Content Hugging Priority
=]
movieImageView.setContentHuggingPriority(.defaultHigh, for: .horizontal)
如果你想设置titleLabel
的文字宽度并且比imageView更耐看,你应该设置titleLabel
[=79的Compression Resistance Priority
=]
titleLabel.setContentCompressionResistancePriority(.defaultHigh, for: .horizontal)
奖金
您已经设置了您的 movieImageView 的底部锚点和顶部锚点,这间接说明了您的 movieImageView 的高度。
设置高度后,您再次将高度更改为某个常数值 100。
要避免此警告,请删除
movieImageView.bottomAnchor.constraint(equalTo: self.bottomAnchor).isActive = true
这个警告是因为相同 属性.
的冗余约束
你试试下面的约束
movieImageView.topAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.topAnchor).isActive = true
movieImageView.leadingAnchor.constraint(equalTo: self.view.leadingAnchor).isActive = true
movieImageView.widthAnchor.constraint(equalToConstant: 100).isActive = true
movieImageView.heightAnchor.constraint(equalToConstant: 100).isActive = true
titleLabel.leadingAnchor.constraint(equalTo: movieImageView.trailingAnchor, constant: 20).isActive = true
titleLabel.topAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.topAnchor).isActive = true
titleLabel.trailingAnchor.constraint(equalTo: self.view.trailingAnchor).isActive = true
titleLabel.bottomAnchor.constraint(equalTo: self.movieImageView.bottomAnchor).isActive = true
这个约束的结果是
我有一段代码可以根据提供的约束按预期显示图像视图和 UILabel side.It,但控制台中有很多警告
这里是错误
[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:0x6000003d0190 V:|-(0)-[UIImageView:0x7faa7401b940] (active, names: '|':Movies_TODO.MovieViewCell:0x7faa7402caa0'custom' )>",
"<NSLayoutConstraint:0x6000003d05a0 UIImageView:0x7faa7401b940.bottom == Movies_TODO.MovieViewCell:0x7faa7402caa0'custom'.bottom (active)>",
"<NSLayoutConstraint:0x6000003d0640 UIImageView:0x7faa7401b940.height == 100 (active)>",
"<NSLayoutConstraint:0x6000003e7070 'UIView-Encapsulated-Layout-Height' Movies_TODO.MovieViewCell:0x7faa7402caa0'custom'.height == 100.5 (active)>"
)
将尝试通过打破约束来恢复
这是我的一段代码
self.addSubview(movieImageView)
self.addSubview(titleLabel)
movieImageView.topAnchor.constraint(equalTo: self.topAnchor).isActive = true
movieImageView.leadingAnchor.constraint(equalTo: self.leadingAnchor).isActive = true
movieImageView.bottomAnchor.constraint(equalTo: self.bottomAnchor).isActive = true
movieImageView.widthAnchor.constraint(equalToConstant: 100).isActive = true
movieImageView.heightAnchor.constraint(equalToConstant: 100).isActive = true
titleLabel.leadingAnchor.constraint(equalTo: movieImageView.trailingAnchor, constant: 20).isActive = true
titleLabel.topAnchor.constraint(equalTo: self.topAnchor).isActive = true
titleLabel.trailingAnchor.constraint(equalTo: self.trailingAnchor).isActive = true
titleLabel.bottomAnchor.constraint(equalTo: self.bottomAnchor).isActive = true
您的 movieImageView
具有类似 height equal to 100
和 height equal to super view's height
的约束(这是因为您添加了顶部和底部锚点约束)。
1 - 如果你想从上到下设置 movieImageView
只需删除 movieImageView.heightAnchor.constraint(equalToConstant: 100).isActive = true
.
2 - 如果你想将 movieImageView
的高度设置为 100,只需删除你不想为 movieImageView
->
// if you want to stick your imageView to bottom delete this
movieImageView.topAnchor.constraint(equalTo: self.topAnchor).isActive = true
// if you want to stick your imageView to top delete this
movieImageView.bottomAnchor.constraint(equalTo: self.bottomAnchor).isActive = true
和UIStackView
- 如果你想创建一个
TableViewCell
有左侧图像和右侧标签,你可以使用StackView
并且你可以不受任何限制地做你想做的事。
创建一个UIStackView
let stackView = UIStackView()
添加你的movieImageView
和titleLabel
stackView.addArrangedSubviews(movieImageView, titleLabel)
stackView.axis = .horizontal
stackView.spacing = 20.0
将您的 stackView
添加到 super.view
self.addSubview(stackView)
stackView.translatesAutoresizingMaskIntoConstraints = false
stackView.topAnchor.constraint(equalTo: self.topAnchor).isActive = true
stackView.bottomAnchor.constraint(equalTo: self.bottomAnchor).isActive = true
stackView. trailingAnchor.constraint(equalTo: self. trailingAnchor).isActive = true
stackView.leadingAnchor.constraint(equalTo: self.leadingAnchor).isActive = true
如果你想根据它的图像设置movieImageView
的宽度,你应该设置movieImageView
[=40的Content Hugging Priority
=]
movieImageView.setContentHuggingPriority(.defaultHigh, for: .horizontal)
如果你想设置titleLabel
的文字宽度并且比imageView更耐看,你应该设置titleLabel
[=79的Compression Resistance Priority
=]
titleLabel.setContentCompressionResistancePriority(.defaultHigh, for: .horizontal)
奖金
您已经设置了您的 movieImageView 的底部锚点和顶部锚点,这间接说明了您的 movieImageView 的高度。
设置高度后,您再次将高度更改为某个常数值 100。
要避免此警告,请删除
movieImageView.bottomAnchor.constraint(equalTo: self.bottomAnchor).isActive = true
这个警告是因为相同 属性.
的冗余约束你试试下面的约束
movieImageView.topAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.topAnchor).isActive = true
movieImageView.leadingAnchor.constraint(equalTo: self.view.leadingAnchor).isActive = true
movieImageView.widthAnchor.constraint(equalToConstant: 100).isActive = true
movieImageView.heightAnchor.constraint(equalToConstant: 100).isActive = true
titleLabel.leadingAnchor.constraint(equalTo: movieImageView.trailingAnchor, constant: 20).isActive = true
titleLabel.topAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.topAnchor).isActive = true
titleLabel.trailingAnchor.constraint(equalTo: self.view.trailingAnchor).isActive = true
titleLabel.bottomAnchor.constraint(equalTo: self.movieImageView.bottomAnchor).isActive = true
这个约束的结果是