systemLayoutSizeFitting总是返回零

问题描述 投票:0回答:1

基于Apple的docsystemLayoutSizeFitting应该在返回最佳大小时尊重当前对UIView元素的约束。但是,每当我运行以下代码时,我会为{0, 0}获取UIView.layoutFittingCompressedSize,为{1000, 1000}输入获得UIView.layoutFittingExpandedSizeSize

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)获得正确的价值。

ios autolayout
1个回答
0
投票

文档似乎缺乏这一点。

似乎.systemLayoutSizeFitting高度依赖于元素的.intrinsicContentSize。在UIView的情况下,它没有内在的内容大小(除非你已经覆盖它)。

因此,如果相关约束是另一个约束的百分比,则.systemLayoutSizeFitting(UIView.layoutFittingCompressedSize)将返回{0, 0}。我收集这是因为相关的约束可以改变(为零),因此最小值实际上是零。

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

例如,如果您的子视图是UILabel,那么该元素将具有内在大小,而.systemLayoutSizeFitting应该返回您期望的大小值。

以下是使用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)
© www.soinside.com 2019 - 2024. All rights reserved.