如何在处理多个ViewControllers的UIScrollView上拥有共享背景?

时间:2017-03-17 21:43:27

标签: ios swift

我想制作1个UIScrollView,里面有1个背景,可供多个ViewControllers使用。当用户滚动到下一个ViewController时,背景应该随之滚动。这是我的代码:

override func viewDidLoad() {
        super.viewDidLoad()
        let leftViewController: StartLeftViewController = StartLeftViewController(nibName: "StartLeftViewController", bundle: nil)
        self.addChildViewController(leftViewController)
        self.theScrollView.addSubview(leftViewController.view)
        leftViewController.didMove(toParentViewController: self)

        let middleViewController: StartMiddleViewController = StartMiddleViewController(nibName: "StartMiddleViewController", bundle: nil)
        self.addChildViewController(middleViewController)
        self.theScrollView.addSubview(middleViewController.view)
        middleViewController.didMove(toParentViewController: self)

        let rightViewController: StartRightViewController = StartRightViewController(nibName: "StartRightViewController", bundle: nil)
        self.addChildViewController(rightViewController)
        self.theScrollView.addSubview(rightViewController.view)
        rightViewController.didMove(toParentViewController: self)

        var middleFrame : CGRect = middleViewController.view.frame
        middleFrame.origin.x = self.view.frame.width
        middleViewController.view.frame = middleFrame

        var thirdFrame : CGRect = rightViewController.view.frame
        thirdFrame.origin.x = 2 * self.view.frame.width
        rightViewController.view.frame = thirdFrame

        self.theScrollView.contentSize = CGSize(width: self.view.frame.width * 3, height: self.view.frame.height)

        theScrollView.contentOffset.x = view.frame.width

    }

我已经尝试在middleViewController中设置一个宽度为3x的背景,但是我在向左/右ViewController添加元素时遇到了问题,导致元素隐藏在背景后面。谢谢。

1 个答案:

答案 0 :(得分:0)

在故事板中,您可以将三个容器视图放置在滚动视图中,一个接一个地水平放置,并将每个容器视图与嵌入segue连接到相应的视图控制器。