[英]Swift - Scroll a UIScrollView by tapping button inside a ViewController inside the ScrollView
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. 我有一个UIScrollView,其中包含一堆视图控制器,并且在每个视图控制器中都有一个按钮,如果按下该按钮,应将UIScrollView滚动到某个位置。 How do I go about connecting the viewController IBAction for the button to set the scroll position of the UIScrollView?
如何连接按钮的viewController IBAction以设置UIScrollView的滚动位置?
I know I'll call contentOffset at some point, but I'm struggling with getting the ViewControllers to control the Scroll View. 我知道我会在某个时候调用contentOffset,但是我在努力让ViewControllers控制滚动视图。
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
. 一种可能的方式...将每个
UIViewController.view
添加到UIScrollView
还可以在每个引用UIScrollView
UIViewController
中设置一个var
。
For example, for some UIViewController
, we'll call it X, put 例如,对于某些
UIViewController
,我们将其称为X,
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: 然后,在控制
UIScrollView
类中,将视图X添加到UIScrollView
,还请执行以下操作:
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. 现在,当在
viewControllerX
调用IBAction
方法时,您就具有对UIScrollView
的引用,并且正如您所说,可以执行self.scrollView.contentOffset.y = someValueHere
更改其位置。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.