简体   繁体   English

使用约束基于内部视图高度设置UIView高度

[英]Set UIView height based on inner view height using constraints

I have one UIView and inside that, N UILabels which are laid out relative to each other. 我有一个UIView ,里面有N个 UILabels ,它们相对于彼此布置。

The containing UIView has a background color, I want to extend the UIView to be high enough to cover all labels inside it, so the background color is behind them all. 包含的UIView具有背景色,我想将UIView扩展到足够高以覆盖其中的所有标签,因此背景色位于它们的后面。

(I'm embedding them in a UIView so I can have the labels inset from the view edges.) (我将它们嵌入到UIView中,因此可以从视图边缘插入标签。)

Is there away to make the UIView 's height expand to fill its content? 是否有办法使UIView的高度扩展以填充其内容? I can't figure it from the constraint options, it seems like its all relative to superviews. 我不能从约束选项中看出来,似乎它全都与超级视图有关。

Normally I'd just work out the frame sizes programatically in viewDidAppear but those are getting reset by the constraints system, AFAIK. 通常,我只是在viewDidAppear编程方式计算出帧的大小,但是约束系统AFAIK已将其重置。

I think I actually worked it out though. 我想我实际上已经解决了。

I had the labels height set manually from when I drag-dropped and resized it. 从拖放和调整大小后,我便手动设置了标签高度。 Deleting the height constraint on the UILabel made it size to fit content, which causes its superview to resize too. 删除UILabel上的高度约束后,其大小将适合内容,这也将导致其父视图也随之调整大小。 At least I think that's the case, I'm new to constraints. 至少我认为是这种情况,我是约束的新手。

Will leave the question up since it will probably bite someone else too. 因为它也可能会咬别人,所以会留下问题。

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

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