1
votes

I have a UIScrollView that contains a bunch of view controllers, and in each view controller there is a button that if pressed, should scroll the UIScrollView to a certain position. How do I go about connecting the viewController IBAction for the button to set the scroll position of the UIScrollView?

I know I'll call contentOffset at some point, but I'm struggling with getting the ViewControllers to control the Scroll View.

1
In viewContoler,self.view.superview is not the scrollVIew?Leo

1 Answers

1
votes

One way you could do it... When you add each UIViewController.view to the UIScrollView you could also set a var in each UIViewController that references the UIScrollView.

For example, for some UIViewController, we'll call it X, put

var scrollView: UIScrollView?

in it. Then in your class that controls the UIScrollView, when you add view X to the UIScrollView, also do the following:

viewControllerX.scrollView = self.theScrollView

Now, when your IBAction method is called in viewControllerX, you have a reference to the UIScrollView and as you said, can do self.scrollView.contentOffset.y = someValueHere to change its position.