ios 如何在 Swift 中设置自定义视图的内在内容大小?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/36280699/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to set a custom view's intrinsic content size in Swift?
提问by Suragch
Background
背景
I am making a vertical label to use with traditional Mongolian script. Before I was just rotating a UILabel
but there were some performance issues and other complications with this. Now I am working on making a label from scratch. However, I need the vertical label to tell auto layout when its height adjusts (based on string length).
我正在制作一个垂直标签以与传统的蒙古文字一起使用。在我只是旋转 a 之前,UILabel
但是有一些性能问题和其他并发症。现在我正在从头开始制作标签。但是,我需要垂直标签在高度调整时告诉自动布局(基于字符串长度)。
What I have read
我读过的
I read the Intrinsic Content Sizeand Views with Intrinsic Content Sizedocumentation. These were more about how to use it, though, and not how to define it in a custom view.
我阅读了Intrinsic Content Size和Views with Intrinsic Content Size文档。不过,这些更多是关于如何使用它,而不是如何在自定义视图中定义它。
Searching for "ios intrinsic content size for a custom view" only gives me
搜索“自定义视图的 ios 内在内容大小”只会给我
in Stack Overflow. This particular question didn't even need intrinsic content size because their view was just an assembly of standard views.
在堆栈溢出中。这个特定的问题甚至不需要内在的内容大小,因为他们的视图只是标准视图的集合。
What I am trying
我在尝试什么
What I am trying is my answer below. I am adding this Q&A pair so that it won't take other people as long to find the answer as it took me with the search keywords that I used.
我正在尝试的是我在下面的答案。我正在添加这个问答对,这样其他人就不会像使用我使用的搜索关键字那样花费很长时间才能找到答案。
回答by Suragch
Setting the intrinsic content size of a custom view lets auto layout know how big that view would like to be. In order to set it, you need to override intrinsicContentSize
.
设置自定义视图的内在内容大小可以让自动布局知道该视图想要有多大。为了设置它,您需要覆盖intrinsicContentSize
.
override var intrinsicContentSize: CGSize {
return CGSize(width: x, height: y)
}
Then call
然后打电话
invalidateIntrinsicContentSize()
Whenever your custom view's intrinsic content size changes and the frame should be updated.
每当您的自定义视图的内在内容大小发生变化并且框架应更新时。
Notes
笔记
- Swift 3 update: Easier Auto Layout: Coding Constraints in iOS 9
- Just because you have the intrinsic content size set up in your custom view doesn't mean it will work as you expect. Read the documentationfor how to use it, paying special attention to Content-Hugging and Compression-Resistance.
- Thanks also to this Q&A for putting me on the right track: How can I add padding to the intrinsic content size of UILabel?
- Thanks also to this articleand the documentationfor help with
invalidateIntrinsicContentSize()
.
- Swift 3 更新:更简单的自动布局:iOS 9 中的编码约束
- 仅仅因为您在自定义视图中设置了内在内容大小并不意味着它会按您的预期工作。阅读文档以了解如何使用它,特别注意Content-Hugging 和 Compression-Resistance。
- 还要感谢这个问答让我走上了正确的轨道:如何向 UILabel 的内在内容大小添加填充?
- 还要感谢这篇文章和帮助文档
invalidateIntrinsicContentSize()
。
回答by Fattie
Example of a "view with intrinsic height" ...
“具有固有高度的视图”的示例......
@IBDesignable class HView: UIView {
@IBInspectable var height: CGFloat = 100.0
override var intrinsicContentSize: CGSize {
return CGSize(width: 99, height: height)
// if using in, say, a vertical stack view, the width is ignored
}
override func prepareForInterfaceBuilder() {
invalidateIntrinsicContentSize()
}
}
which you can set as an inspectable
您可以将其设置为可检查的
Since it has an intrinsic height, it can (for example) be immediately inserted in a stack view in code:
由于它具有固有高度,因此可以(例如)在代码中立即插入到堆栈视图中:
stack?.insertArrangedSubview(HView(), at: 3)
In contrast, if it was a normal view with no intrinsic height, you'd have to add a height anchor or it would crash:
相反,如果它是没有固有高度的普通视图,则必须添加高度锚点,否则会崩溃:
let v:UIView = HView()
v.heightAnchor.constraint(equalToConstant: 100).isActive = true
stack?.insertArrangedSubview(v, at: 3)
Note that in ...
请注意,在...
the important special case of a stack view:
堆栈视图的重要特例:
- you set only ONEanchor (for vertical stack view, the height; for horizontal the width)
- 您只设置一个锚点(对于垂直堆栈视图,高度;对于水平宽度)
so, setting the intrinsic height works perfectly, since:
因此,设置固有高度非常有效,因为:
- the intrinsic height indeed means that the height anchor specifically will be set automatically if needed.
- 固有高度确实意味着高度锚点将在需要时自动设置。
Remembering that in all normal cases of a subview, many other anchors are needed.
请记住,在子视图的所有正常情况下,还需要许多其他锚点。