如何与内在尺寸一起工作

如何与内在尺寸一起工作

本文介绍了addSubview 如何与内在尺寸一起工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下代码的结果是:我没有看到标签.

我的理解是标签的内在尺寸将允许标签拉伸.为什么没有发生这种情况?

导入 UIKit类视图控制器:UIViewController {覆盖 func viewDidLoad() {super.viewDidLoad()view.addSubview(标签)}让标签: UILabel = {让标签 = UILabel()label.text = "你今天过得好吗我的朋友"label.backgroundColor = .orange退货标签}()}
解决方案

这个:

let label = UILabel()

(0, 0) 处创建一个标签,宽度为 0,高度为 0.默认情况下,iOS 将从 frame 为标签的位置、宽度和高度创建约束.

如果您想使用标签的固有大小,请禁用从框架创建约束并提供标签约束以将其放置在视图中.

例如(添加标签作为子视图后):

label.translatesAutoresizingMaskIntoConstraints = falselabel.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = truelabel.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true

或者您可以在创建框架时使用 label.intrisicContentSize:

label.frame = CGRect(origin: CGPoint(x: 40, y: 100), size: label.intrinsicContentSize)

注意:如果你使用label.intrinsicSize来设置frame,当你改变标签文本时frame不会改变.您必须再次更新它:

label.frame.size = label.intrinsicContentSize

更改label.text后.

实际上,自动布局为标签的 instrinsicContentSize 创建了 4 个约束,但这些约束的优先级低于从 frame 创建的约束,因此它们没有影响.通过禁止从 frame 创建约束,你给了较低优先级的 instrinsicContentSize 约束一个机会来影响 UILabel 的布局.>

您可以在 Apple 的自动布局指南,标题为内在内容大小.

The result of the following code is: I don't see the label.

My understanding was that the intrinsicSize of the label would allow the label to stretch . Why isn't that happening?

import UIKit

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        view.addSubview(label)
    }

    let label : UILabel = {
        let label = UILabel()
        label.text = "How are you doing today my friend"
        label.backgroundColor = .orange

        return label
    }()
}
解决方案

This:

let label = UILabel()

creates a label at (0, 0) with width 0 and height 0. By default, iOS will create constraints from the frame for the position, width, and height of the label.

If you want to use the intrinsic size of the label, disable the creation of constraints from the frame and give the label constraints to place it in the view.

For example (after adding the label as a subview):

label.translatesAutoresizingMaskIntoConstraints = false
label.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
label.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true

Or you can use the label.intrisicContentSize when you create frame:

label.frame = CGRect(origin: CGPoint(x: 40, y: 100), size: label.intrinsicContentSize)

Note: If you use label.intrinsicSize to set the frame, the frame will not change when you change the label text. You'd have to update it again with:

label.frame.size = label.intrinsicContentSize

after changing label.text.


In reality, Auto Layout creates 4 constraints for a label's instrinsicContentSize, but these constraints are at a lower priority than the constraints created from the frame, so they have no effect. By disabling the creation of constraints from the frame, you give the lower priority instrinsicContentSize constraints an opportunity to affect the layout of the UILabel.

You can read about this in more detail in Apple's Auto Layout Guide in the section entitled Intrinsic Content Size.

这篇关于addSubview 如何与内在尺寸一起工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-11 17:01