Lor*_*ush 14 ios autolayout swift
我systemLayoutSizeFittingSize用来获得满足其内部约束的自定义视图子类的最小大小.
let someView = SomeView()
someView.setNeedsUpdateConstraints()
someView.setNeedsLayout()
someView.layoutIfNeeded()
let viewSize = someView.systemLayoutSizeFittingSize(UILayoutFittingCompressedSize)
println(viewSize) // PRINTS (0.0, 96.0) WHICH IS RIGHT!
Run Code Online (Sandbox Code Playgroud)
我得到正确的值,但我也收到Unable to simultaneously satisfy constraints警告:
"<NSLayoutConstraint:0x7ff16b753190 '_UITemporaryLayoutHeight' V:[ProjectName.SomeView:0x7ff16b772210(0)]>",
"<NSLayoutConstraint:0x7ff16b75de50 UIImageView:0x7ff16b76eff0.top == ProjectName.SomeView:0x7ff16b772210.topMargin>",
"<NSLayoutConstraint:0x7ff16b7653f0 UIImageView:0x7ff16b76eff0.bottom <= ProjectName.SomeView:0x7ff16b772210.bottomMargin>"
Run Code Online (Sandbox Code Playgroud)
下面是我的SomeViewUIView子类.
它只包含固定在顶部,左侧和底部边距的80x80 imageView.(我使用PureLayout编写约束).
现在很明显,这个视图的固定高度为96(边距为80 + 8x2)但理论上如果它的子视图改变了大小,它可以拉伸.
有任何想法吗?搜索谷歌UITemporaryLayoutHeight(或宽度)给出0结果...
class SomeView: UIView {
let imageView = UIImageView()
var constraintsSet = false
override init(frame: CGRect) {
super.init(frame: frame)
backgroundColor = UIColor.lightGrayColor()
imageView.backgroundColor = UIColor.darkGrayColor()
addSubview(imageView)
}
override func updateConstraints() {
if(!constraintsSet) {
imageView.autoPinEdgeToSuperviewMargin(.Top)
imageView.autoPinEdgeToSuperviewMargin(.Left)
imageView.autoPinEdgeToSuperviewMargin(.Bottom, relation: NSLayoutRelation.GreaterThanOrEqual)
imageView.autoSetDimension(.Width, toSize: 80.0)
imageView.autoSetDimension(.Height, toSize: 80.0)
constraintsSet = true
}
super.updateConstraints()
}
}
Run Code Online (Sandbox Code Playgroud)
Sta*_*org 31
我已经注意到,如果我layoutIfNeeded在父母布局之前调用了一个视图,就会发生这种情况.我敢打赌如果你删除someView.layoutIfNeeded()你就不会再看到那个错误.您可能也可以摆脱someView.setNeedsLayout()并替换它someView.updateConstraintsIfNeeded().systemLayoutSizeFittingSize不应该要求实际布置视图,只需要正确配置约束.
Ale*_*cić 19
这个:
它只包含固定在顶部,左侧和底部边距的80x80 imageView
还有这个
NSLayoutConstraint:0x7ff16b753190'_UITemporaryLayoutHeight'V:[ProjectName.SomeView:0x7ff16b772210(0)]
是问题的根源.UIKit在加载/布局/显示过程中的某个点使用高度为0.由于您对顶部和底部都有约束并且 imageview具有其固有内容大小,因此您告诉它将图像拟合到零垂直空间.
解决此问题的常用方法是将top或bottom约束的优先级设置为小于1000的任何值.
小智 5
当视图没有superview并且layoutIfNeeded被调用时,我收到了此警告.我的解决方案是layoutIfNeeded在视图添加到a后调用superview.我希望这有帮助:
- (void)didMoveToSuperview {
[super didMoveToSuperview];
[self layoutIfNeeded];
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4331 次 |
| 最近记录: |