简体   繁体   English

systemLayoutSizeFitting总是返回零

[英]systemLayoutSizeFitting returns zero always

Based on Apple's doc , systemLayoutSizeFitting is supposed to respect current constraints on the UIView element when returning the optimal size. 基于Apple的docsystemLayoutSizeFitting应该在返回最佳大小时尊重UIView元素的当前约束。 However, whenever I run following code, I would get {0, 0} for UIView.layoutFittingCompressedSize and {1000, 1000} for UIView.layoutFittingExpandedSizeSize input. 但是,每当我运行以下代码时,我会为UIView.layoutFittingCompressedSize获取{0, 0} {1000, 1000}UIView.layoutFittingExpandedSizeSize输入获得{0, 0} UIView.layoutFittingCompressedSize {1000, 1000}

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)

I noticed that if I change the width constraint to something constants, then I'd get a valid value from systemLayoutSizeFitting . 我注意到如果我将width约束更改为常量,那么我将从systemLayoutSizeFitting获得一个有效值。 Trying to understand why such behavior is happening and if it's possible to get a right value from systemLayoutSizeFittingSize(_ size: CGSize) . 试图理解为什么会发生这种行为,以及是否可以从systemLayoutSizeFittingSize(_ size: CGSize)获得正确的值。

Documentation seems rather lacking on this one. 文档似乎缺乏这一点。

It appears that .systemLayoutSizeFitting is highly dependent on the .intrinsicContentSize of the element. 似乎.systemLayoutSizeFitting高度依赖于元素的.intrinsicContentSize In the case of a UIView , it has no intrinsic content size (unless you've overridden it). UIView的情况下,它没有内在的内容大小(除非你重写它)。

So, if the related constraint is a percentage of another constraint, .systemLayoutSizeFitting(UIView.layoutFittingCompressedSize) will return {0, 0} . 因此,如果相关约束是另一个约束的百分比 ,则.systemLayoutSizeFitting(UIView.layoutFittingCompressedSize)将返回{0, 0} I gather this is because the related constraint could change (to Zero), therefor the minimum value is, in fact, Zero. 我收集这是因为相关的约束可以改变(为零),因此最小值实际上是零。

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. 如果将.width约束更改为常量(例如mainView.frame.width * 0.3333 ),那么您将获得有效的大小值,因为常量宽度约束将成为固有宽度。

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 ,则该元素具有内在大小,而.systemLayoutSizeFitting应返回您期望的大小值。

Here's an example using a UILabel that will demonstrate: 以下是使用UILabel的示例,该示例将演示:

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM