UIScrollView dynamically resize UILabel

2019-08-21 14:43发布

问题:

i have got a UILabel which is not static.

I want to resize my scroll view so that it fits the label.

Here is my idea now:

self.scrollView.contentSize = CGSizeMake(320.0, 92+self.contentLabel.frame.size.height);

92 stands for pixels from where my label start. (there is a heading too)

But it doesn't work, it seems to be connected with Interface Builder also.

Thanks guys.

回答1:

You need to set the frame as well. The contentSize of the scrollview is just what is within, not the frame of the view itself. If the contentSize is greater than the frame it will result in scrolling.

CGSize buttonSize =  CGSizeMake(320.0, 92+self.contentLabel.frame.size.height);
self.scrollView.contentSize = buttonSize;
self.scrollView.frame = CGSizeMake(0, 0, 320.0, buttonSize.width, buttonSize.height);