Question

I have a view hierarchy like this:

-First View Controller
  -View
    -ScrollView
      -TBD
    -ImageView

I am trying to use the following (from apple docs) to set the content size of the ScrollView:

UIScrollView *tempScrollView=(UIScrollView *)self.view;
tempScrollView.contentSize=CGSizeMake(320,1000);

but I get error:

Terminating app due to uncaught exception 'NSInvalidArgumentException'

How can I modify to correctly set contentSize for the scrollView?

Was it helpful?

Solution

So the problem is this

self.view gets your main UIView for the controller, your scrollView is subview of self.view and to access this you'll need an IBOutlet.

So first thing is to create the IBOutlet in your viewControllers .h

Create an IBOutlet for UIScrollView

Then in your storyboard hook your scrollView that's ypou put in your viewContoller to the IBoulet. Click the scrollView -> One right Utilities tab -> slick the right most tab, it will have a arrow in a circle -> Then click the Referencing Outlet and drag it over to your view controller -> it will give you an option to either select the scrollView or view.

Hook the IBoulet up

Select scrollView.

Select ScrollView

That's it, now where you have your code just change it to:

   self.scrollView.contentSize = CGSizeMake(320,1000);

OTHER TIPS

If you are using Auto Layout, then instead of setting the contentSize of the scrollView, you need to set constraints so that all the subviews inside the scrollView. Then your contentSize will be determined automatically according to your constraints.

Read this post for more details.

Your view controller's view is not a scroll view, so the exception occurs. Possible remedies 1. Change custom class of your view controller's view's class to UIScrollView tnen do like you did 2. Set your scrollview's tag to 1000 then do as follows

UIScrollView *tempScrollView=(UIScrollView *)[self.view viewWithTag:1000];
tempScrollView.contentSize=CGSizeMake(320,1000);

1000 is not mandatory, tou can put any integer, but make your that no other views of it or siblings have that tag

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top