systemLayoutSizeFitting始终返回零 [英] systemLayoutSizeFitting returns zero always
问题描述
基于Apple的 doc ,UIView
元素上的当前约束.但是,每当我运行以下代码时,对于UIView.layoutFittingCompressedSize
输入,我将得到{0, 0}
,对于UIView.layoutFittingExpandedSizeSize
输入,我将得到{1000, 1000}
.
Based on Apple's doc, systemLayoutSizeFitting
is supposed to respect current constraints on the UIView
element when returning the optimal size. However, whenever I run following code, I would get {0, 0}
for UIView.layoutFittingCompressedSize
and {1000, 1000}
for UIView.layoutFittingExpandedSizeSize
input.
let mainView = UIView(frame: CGRect(origin: .zero, size: CGSize(width: 375, height: 50)))
mainView.backgroundColor = .red
PlaygroundPage.current.liveView = mainView
let subview = UIView()
subview.backgroundColor = .yellow
mainView.addSubview(subview)
subview.snp.makeConstraints { make in
make.width.equalToSuperview().dividedBy(3.0)
make.left.top.bottom.equalToSuperview()
}
mainView.setNeedsLayout()
mainView.layoutIfNeeded()
subview.frame
subview.systemLayoutSizeFitting(UIView.layoutFittingCompressedSize)
我注意到,如果将width
约束更改为常量,那么我将从systemLayoutSizeFitting
获得有效值.试图了解为什么会发生这种行为,以及是否有可能从systemLayoutSizeFittingSize(_ size: CGSize)
获取正确的值.
I noticed that if I change the width
constraint to something constants, then I'd get a valid value from systemLayoutSizeFitting
. Trying to understand why such behavior is happening and if it's possible to get a right value from systemLayoutSizeFittingSize(_ size: CGSize)
.
推荐答案
此文档似乎缺少文档.
看来.systemLayoutSizeFitting
高度依赖于元素的.intrinsicContentSize
.对于UIView
,它没有固有的内容大小(除非您已覆盖它).
It appears that .systemLayoutSizeFitting
is highly dependent on the .intrinsicContentSize
of the element. In the case of a UIView
, it has no intrinsic content size (unless you've overridden it).
因此,如果相关约束是另一个约束的 percentage ,则.systemLayoutSizeFitting(UIView.layoutFittingCompressedSize)
将返回{0, 0}
.我认为这是因为相关约束可能会更改(变为零),因此最小值实际上是零.
So, if the related constraint is a percentage of another constraint, .systemLayoutSizeFitting(UIView.layoutFittingCompressedSize)
will return {0, 0}
. I gather this is because the related constraint could change (to Zero), therefor the minimum value is, in fact, Zero.
如果将.width
约束更改为常量(例如mainView.frame.width * 0.3333
),则由于常量宽度约束变为固有宽度,您将获得有效的大小值.
If you change your .width
constraint to a constant (such as mainView.frame.width * 0.3333
) then you'll get a valid size value, as the constant width constraint becomes the intrinsic width.
例如,如果子视图是UILabel
,则该元素将具有固有尺寸,而.systemLayoutSizeFitting
应返回您期望的尺寸值.
If your subview is a UILabel
, for example, that element will have an intrinsic size, and .systemLayoutSizeFitting
should return a size value that you'd expect.
以下是使用UILabel
的示例,该示例将演示:
Here's an example using a UILabel
that will demonstrate:
import UIKit
import PlaygroundSupport
let mainView = UIView(frame: CGRect(origin: .zero, size: CGSize(width: 375, height: 50)))
mainView.backgroundColor = .red
PlaygroundPage.current.liveView = mainView
let v = UILabel()
v.text = "Testing"
v.translatesAutoresizingMaskIntoConstraints = false
v.backgroundColor = .green
mainView.addSubview(v)
NSLayoutConstraint.activate([
v.widthAnchor.constraint(equalTo: mainView.widthAnchor, multiplier: 3.0 / 10.0),
v.leftAnchor.constraint(equalTo: mainView.leftAnchor),
v.topAnchor.constraint(equalTo: mainView.topAnchor),
v.bottomAnchor.constraint(equalTo: mainView.bottomAnchor),
])
mainView.setNeedsLayout()
mainView.layoutIfNeeded()
v.frame
v.systemLayoutSizeFitting(UIView.layoutFittingCompressedSize)
这篇关于systemLayoutSizeFitting始终返回零的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!