Can't get UIScrollView to work

不想你离开。 提交于 2019-12-25 12:27:20

问题


This is a storyboard app. I added a UIScrollView on top of the UIView that is there in the view controller by default. Then I added some UI elements on to the scroll view. After designing the viewable part of the Scroll view, using its handles I stretched it vertically and pushed it up a bit so that that part is visible for me to design the rest of the screen. After I was done I positioned the scroll view back to fit the view. Please note that I did not resize the scroll view to match the size of the view from the IB.

Then I added all 4 constraints to the scroll view (Leading and Trailing space to superview, Top and Bottom space to superview). Then I selected the UIView, opened up the size inspector and changed the Bottom Space to superview's value to 0. That automatically resized the UIScrollView to fit inside the view controller.

Then I added the following lines of code inside the viewDidLoad method.

self.scrollView.contentSize = CGSizeMake(self.view.frame.size.width, self.scrollView.frame.size.height);
self.scrollView.frame = self.view.frame;

But this does not work. The UIScrollView won't scroll. Yes, the scrolling is enabled.

Can anyone please tell me what I might be missing here?

Thank you.

EDIT:

I found an old project of mine here. It utilize the way I just explained above and it works! No idea why it doesn't anymore.

I also added a demo project here with the issue.


回答1:


If I understood your code correctly (a screenshot of the view would help) you set the scrollviews frame to be full size of the view and set then contentSize to the view's size. A scrollview scrolls only if the contentSize is larger than its size.




回答2:


This might not be the complete answer to your question, but it will help you and others for sure.

To design the views that are bigger in size than the size of the UIScrollView, the best way to have the UIView as a child view to UIScrollView and put all the content on the UIView and finally place the UIView as subview of the UISCrollView. AT last you can set the size of the UIView as the content size of the UISCrollView.

Cheers. :)



来源:https://stackoverflow.com/questions/21363529/cant-get-uiscrollview-to-work

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!