简体   繁体   English

使用AutoLayout动态布局scrollview contentSize

[英]Dynamically layout scrollview contentSize with AutoLayout

I have a simple case: I have the following views hierarchy: 我有一个简单的案例:我具有以下视图层次结构:

  1. View 视图
    1. ScrollView 滚动视图
      1. View 视图
        1. Label 标签

My label is positioned at the bottom of it's super view, and it's height is changed dynamically, depending ot the size of the text that it is rendering. 我的标签位于其超级视图的底部,并且它的高度是动态更改的,具体取决于所渲染文本的大小。

My aim is to adjust the label size depending on the text, so that no text is truncated, and with the growth of the lines of the label, to grow the scrollview's content size, so that the label always is positioned at the bottom. 我的目标是根据文本调整标签大小,以使文本不会被截断,并且随着标签行的增加,滚动视图的内容大小也会增加,以便标签始终位于底部。

How can I do that with autolayouts, preferably from IB only? 如何使用自动布局,最好仅从IB中做到这一点?

If you want the UILabel to stick at the bottom of its parent UIView using autolayout, then that view won't expand its height when the UILabel height increase, what really happens is that the UILabel will move up to occupy more area. 如果您希望UILabel使用自动布局放置在其父UIView的底部,那么当UILabel高度增加时,该视图将不会扩展其高度,实际上发生的是UILabel将向上移动以占据更多区域。

I f you wish your UIView to expand, then don't use autolayout in that UIView, and position your UILabel at a constant origin, then change the view's height & the scrollview height as per the UILabel text. 如果您希望UIView展开,那么不要在该UIView中使用自动布局,而是将UILabel定位在恒定的原点,然后根据UILabel文本更改视图的高度和scrollview的高度。

you can get the UILabel size using the below line of code: 您可以使用以下代码行获取UILabel大小:

CGSize expectedLabelSize = [yourString sizeWithFont:yourLabel.font 
                        constrainedToSize:maximumLabelSize 
                        lineBreakMode:yourLabel.lineBreakMode];

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

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