1

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? Commented Oct 13, 2015 at 0:24

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.