如何将safeAreaLayout与内容视图一起用于页面视图控制器?

时间:2018-08-04 05:05:00

标签: ios swift uipageviewcontroller safearealayoutguide

我的应用程序使用pageviewcontroller来显示随着用户滚动页面而动态更改的内容。

它使用容器视图,该容器视图内部具有页面视图控制器,以及进入页面视图控制器的内容的内容视图:

情节提要显示了3个视图控制器: enter image description here

通过以下功能设置首页:

    func setPageViewControllerFirst(index:Int) {
    //set up a content view controller
    let pageVC = self.storyboard?.instantiateViewController(withIdentifier: "pageSlideVC") as! UIPageViewController

    pageVC.dataSource = self
    pageVC.delegate = self

    //the passed in index is set at currentIndex which is 1 at the beginning
    let firstController = getContentViewController(atIndex: 1)

    pageVC.setViewControllers([firstController], direction: UIPageViewControllerNavigationDirection.forward, animated: true, completion: nil)

    //set up the pageviewcontroller with this created content view
    self.pageViewController = pageVC


    self.addChildViewController(self.pageViewController!)
    self.pageView.addSubview(self.pageViewController!.view)
    self.pageViewController?.didMove(toParentViewController: self)
}

以下功能用于将正确的内容获取到pageviewcontroller视图中:

    //MARK: - Get Content View Controller
func getContentViewController(atIndex index: Int) -> ContentViewController {

    //set up the content view
    let contentViewController = self.storyboard?.instantiateViewController(withIdentifier: "contentViewController") as! ContentViewController

    //make the PresentTestViewController a delegate of the ContentViewController
    //at the time it's created
    //this way it can report back to the PresentTestView controller with the delegate protocol
    contentViewController.delegate = self

    if self.questions.count > 0 {

        //pass along the questions
        contentViewController.questions = questions

        //pass along the test
        if let test = testToPresent {
            contentViewController.testToDisplay = test
        }

        //pass along the episode
        if let episode = episodeToRecord {
            contentViewController.episodeToRecord = episode
        }

        //pass along the question to present
        if let question = questionToPresent {
            contentViewController.questionToDisplay = question
        }

        //iterate through the questions array
        for question in questions {
            //get the displayOrder for the question
            let displayOrder = question.displayOrder
            //if the index passed in matches the displayOrder of the question
            if index == displayOrder {
                //pass along this question to the content view controller
                contentViewController.questionToDisplay = question

                //pass along the size of the pageView
                contentViewController.pageViewDimensions = pageView.bounds

                return contentViewController
            }
        }
    }
    return contentViewController
}

内容视图使用以下内容动态创建一个堆栈视图,该视图被限制为滚动视图:

            scrollView.contentSize.height = 1000


        scrollView.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
        scrollView.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true
        scrollView.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true
        scrollView.trailingAnchor.constraint(equalTo: view.trailingAnchor).isActive = true

        containerStack.topAnchor.constraint(equalTo: scrollView.topAnchor, constant: 20).isActive = true
        containerStack.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: -40).isActive = true
        containerStack.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 10).isActive = true
        containerStack.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -10).isActive = true

我遇到的问题是我似乎找不到滚动视图或堆栈视图使用safeAreaLayout指南的方式,以确保我的堆栈不会与顶部发生冲突在iPhone X上切槽。

enter image description here

我想知道这是否是因为内容视图正在使用pageviewcontroller的大小,而它是从原始视图及其容器视图中获取的大小。

我的问题是:当contentView与原始视图控制器相距3步时,如何使用safeAreaLayout指南。有没有一种方法可以将safeAreaLayout从“ PresentTestViewController”动态传递到pageViewController,然后传递到内容视图,以便可以动态设置contentView的大小和内容的大小? (我希望这不是一个太复杂的问题。我希望有人对此问题感到困惑)。谢谢。

0 个答案:

没有答案