UIScrollView won't work

天涯浪子 提交于 2019-12-08 02:05:42

问题


I've been struggling with this all morning for more than 3 hours now and I'm literally going insane!

I'm creating a Storyboard app for iOS 6 using Xcode 4.6. I dragged and dropped a UIScrollView to a ViewController. 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. But I did not resize the scroll view to match the size of the view from the IB.

I added the following line of code in the viewDidLayoutSubviews method (Used that method instead of viewDidLoad because of Chris' comment here).

self.scrollView.contentSize = self.view.frame.size;

The code gets executed but the scroll bars won't appear and it scrolling does not work at all! I went through almost all the questions and answers here on SO regarding this and tried everything but to no avail.

I attached the runnable Xcode project here as well.

Please tell me what I should do to get this working. I'm eternally frustrated with Apple.

Thank you.


回答1:


It is quite easy though. Remove viewDidLayoutSubviews method and replace viewDidLoad with following code. Height 672 is hardcoded for now.. but it will change as per the content in the scrollview.

- (void)viewDidLoad
{
    [super viewDidLoad];
     self.scrollView.contentSize = CGSizeMake(self.view.frame.size.width, 627);
    self.scrollView.frame = self.view.frame;
}

And in the storyboard, perform following steps 1. Select View and go to size inspector. 2. Select Bottom Space from the contraints. 3. Edit Bottom space constraint and set constant value to 0 (it was -211).

Link is updated source code : http://www54.zippyshare.com/v/37137086/file.html

Hope this helps.




回答2:


UIScrollViews will only scroll if their contentSize is greater than their frame. If it is less than or equal to their frame, they will just act like views.

If you want to test it out, try:

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


来源:https://stackoverflow.com/questions/15516466/uiscrollview-wont-work

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