简体   繁体   English

Swift-通过在ScrollView内的ViewController内点击按钮来滚动UIScrollView

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM