简体   繁体   English

为什么 UIView 的 intrinsicContentSize 总是返回 (-1.0, -1.0)?

[英]Why does intrinsicContentSize for UIView always return (-1.0, -1.0)?

A UIView with proper autolayout is not returning a correct intrinsic content size.具有正确自动布局的 UIView 未返回正确的内在内容大小。 I am getting (-1.0, -1.0) even though I can see that it is sized correctly both in the storyboard and when the app runs.我得到 (-1.0, -1.0) 即使我可以看到它在 storyboard 和应用程序运行时的大小都是正确的。

I have tried creating a Playground that creates a UIView (a) and a UILabel.我已经尝试创建一个创建 UIView (a) 和 UILabel 的 Playground。 I add my label as a subview of (a) and set left, right, top, bottom constraints.我将我的 label 添加为 (a) 的子视图并设置左、右、上、下约束。 I also set some text and verified that my label has a correct intrinsic size (which it does).我还设置了一些文本并验证了我的 label 具有正确的内在大小(确实如此)。 My (a) view returns (-1.0, -1.0) for intrinsic content size.我的 (a) 视图返回 (-1.0, -1.0) 的内在内容大小。

I have also tried creating a new project with a storyboard.我还尝试使用 storyboard 创建一个新项目。 I added a UIView (a) to my view and centered it (X and Y).我在视图中添加了 UIView (a) 并将其居中(X 和 Y)。 I added a UILabel to my (a) view and constrained left, right, top, bottom.我在我的 (a) 视图中添加了一个 UILabel 并限制了左、右、上、下。 I can see that the (a) view sizes correctly.我可以看到(a)视图大小正确。 When I run the app I see that it is still properly sized.当我运行应用程序时,我发现它的大小仍然合适。 My (a) view returns (-1.0, -1.0) for intrinsic content size.我的 (a) 视图返回 (-1.0, -1.0) 的内在内容大小。

I find this extremely strange considering that the UIView does layout according to its intrinsic content size (ie the subviews and associated constraints).考虑到 UIView 根据其内在内容大小(即子视图和相关约束)进行布局,我觉得这非常奇怪。

Assuming that my labels constraints to the edges of my (a) view are all distances of 0, then I would expect my (a) view to have an intrinsic content size equal to that of my label's frame (which is based on the intrinsic size of the label, but that is kinda moot).假设我的 (a) 视图边缘的标签约束都是 0 的距离,那么我希望我的 (a) 视图的内在内容大小等于我的标签框架的内在内容大小(基于内在大小label,但这有点没有意义)。

I genuinely don't understand how a view can set its frame equivalent to the intrinsic size of its subelements while not having an intrinsic content size.我真的不明白视图如何将其框架设置为与其子元素的固有大小相等,而没有固有的内容大小。

(The frame of (a) view returns the correct size, but that doesn't really answer my question. Also - it isn't helpful for my particular situation, but that is outside the scope of this question.) ((a)视图的框架返回正确的大小,但这并不能真正回答我的问题。另外 - 这对我的特定情况没有帮助,但这不在这个问题的 scope 范围内。)

I find this extremely strange considering that the UIView does layout according to its intrinsic content size (ie the subviews and associated constraints).考虑到 UIView 根据其内在内容大小(即子视图和相关约束)进行布局,我觉得这非常奇怪。

This is just a misunderstanding of what the "intrinsic content size" is.这只是对“内在内容大小”的误解。 An ordinary UIView has no (meaningful) instrinsic content size.普通的 UIView 没有(有意义的)内在内容大小。

The intrinsic content size is a property that some interface objects implement (such as UILabel and UIButton) so that you do not have give them height and width constraints.内在内容大小是某些界面对象(例如 UILabel 和 UIButton)实现的属性,因此您不必为它们提供高度和宽度限制。 These are special interface objects that contain text (and/or, in the case of a button, an image), and can size themselves to fit that.这些是包含文本(和/或在按钮的情况下是图像)的特殊界面对象,并且可以调整自身大小以适应它。

Your UIView isn't like that, and basically doesn't implement the intrinsic content size at all.您的 UIView 不是那样的,基本上根本没有实现内在的内容大小。 It is possible to write a UIView subclass that does implement the intrinsic content size, behaving like a UILabel or a UIButton;可以编写实现内在内容大小的 UIView 子类,其行为类似于 UILabel 或 UIButton; but you have not done so.但你还没有这样做。


You say in a comment:你在评论中说:

If I have a UIView with only a Center X and Center Y constraint and given it has some contents, I would like to know what its size is after autolayout operates如果我有一个只有 Center X 和 Center Y 约束的 UIView 并且考虑到它有一些内容,我想知道自动布局运行后它的大小是多少

Fine, but that is not its intrinsic content size.很好,但这不是它固有的内容大小。 It is its size .它是它的大小 If you are in the view controller, implement didLayoutSubviews and look at this view's bounds.size .如果您在视图 controller 中,请实现didLayoutSubviews并查看此视图的bounds.size That is the size you are after.这就是你想要的尺寸。

I'm curious how I could calculate this我很好奇如何计算这个

Ah, ok.喔好吧。 But then you should have asked that.但是你应该问那个。 The answer is: call systemLayoutSizeFittingSize .答案是:调用systemLayoutSizeFittingSize

iOS intrinsicContentSize iOS 内在内容大小

intrinsicContentSize means that view calculates it's size internally (usually in runtime). intrinsicContentSize意味着视图在内部(通常在运行时)计算它的大小。 For example UILabel can find out it's size based on text, font, style...例如UILabel可以根据文本、字体、样式找出它的大小......

iOS operates by explicit and implicit constraints. iOS 通过explicitimplicit约束运行。 Manually you set explicit constrain(in xib or in code).手动设置explicit约束(在xib或代码中)。 AutoLayout converts intrinsicContentSize into implicit constraint. AutoLayout 将intrinsicContentSize转换为implicit约束。

For example basic UIView doesn't have intrinsicContentSize , as an opposite UILable , UIButton has it, that is why it is simple to use them with dynamic content.例如基本UIView没有intrinsicContentSize ,作为相反的UILableUIButton有它,这就是为什么将它们与动态内容一起使用很简单的原因。 For this reason you must set width x height explicitly for UIView因此,您必须为UIView明确设置width x height

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

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