Ios UIPageViewController未调用委托方法(Swift 5)

Ios UIPageViewController未调用委托方法(Swift 5),ios,swift,uipageviewcontroller,Ios,Swift,Uipageviewcontroller,我的项目中有一个非常简单的UIPageViewController设置。当我创建页面视图控制器并导航到它时,一切似乎都正常工作,它正确加载第一个页面。但是,当我尝试滑动或切换到ViewController数组中的其他页面时,我的页面控制器不会调用其委托方法来执行此操作 以下是我如何从父视图实例化和导航到页面视图控制器: let tutorialPageVC = TareTutorialPageViewController() let nc = UINavigationController(roo

我的项目中有一个非常简单的UIPageViewController设置。当我创建页面视图控制器并导航到它时,一切似乎都正常工作,它正确加载第一个页面。但是,当我尝试滑动或切换到ViewController数组中的其他页面时,我的页面控制器不会调用其委托方法来执行此操作

以下是我如何从父视图实例化和导航到页面视图控制器:

let tutorialPageVC = TareTutorialPageViewController()
let nc = UINavigationController(rootViewController: tutorialPageVC)
nc.modalPresentationStyle = .fullScreen
nc.definesPresentationContext = true
nc.setNavigationBarHidden(true, animated: true)
self.present(nc, animated: true, completion: nil)
这是我的页面视图控制器类:

import UIKit

class TareTutorialPageViewController: UIPageViewController, UIPageViewControllerDataSource, UIPageViewControllerDelegate {

    var tutorialPages = [UIViewController]()
    let pageControl = UIPageControl()

    override init(transitionStyle style: UIPageViewController.TransitionStyle, navigationOrientation: UIPageViewController.NavigationOrientation, options: [UIPageViewController.OptionsKey : Any]? = nil) {
        super.init(transitionStyle: .scroll, navigationOrientation: .horizontal, options: [:])

        self.dataSource = self
        self.delegate = self
    }

    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        let initialTutorialPage = 0

        let tutorialPage1 = TutorialPage1ViewController()
        let tutorialPage2 = TutorialPage2ViewController()
        let tutorialPage3 = TutorialPage3ViewController()

        self.tutorialPages.append(tutorialPage1)
        self.tutorialPages.append(tutorialPage2)
        self.tutorialPages.append(tutorialPage3)

        setViewControllers([tutorialPages[initialTutorialPage]], direction: .forward, animated: true, completion: nil)

        self.pageControl.frame = CGRect()
        self.pageControl.currentPageIndicatorTintColor = UIColor.white
        self.pageControl.pageIndicatorTintColor = UIColor.lightGray
        self.pageControl.numberOfPages = self.tutorialPages.count
        self.pageControl.currentPage = initialTutorialPage
        self.view.addSubview(self.pageControl)

        self.pageControl.translatesAutoresizingMaskIntoConstraints = false
        self.pageControl.bottomAnchor.constraint(equalTo: self.view.bottomAnchor, constant: -5).isActive = true
        self.pageControl.widthAnchor.constraint(equalTo: self.view.widthAnchor, constant: -20).isActive = true
        self.pageControl.heightAnchor.constraint(equalTo: self.view.heightAnchor, constant: 20).isActive = true
        self.pageControl.centerXAnchor.constraint(equalTo: self.view.centerXAnchor).isActive = true
    }

    func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController? {

        //Might not need any of this if we only want the user to move forward??
        if let viewControllerIndex = self.tutorialPages.firstIndex(of: viewController)
        {
            if viewControllerIndex == 0
            {
                return self.tutorialPages.last
            }
            else
            {
                return self.tutorialPages[viewControllerIndex-1]
            }
        }

        return nil
    }

    func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController? {

        if let viewControllerIndex = self.tutorialPages.firstIndex(of: viewController)
        {
            if viewControllerIndex < self.tutorialPages.count - 1
            {
                return self.tutorialPages[viewControllerIndex + 1]
            }
            else
            {
                //Navigation to go to scale tare settings here...
                //For now just returns to first page of page view

                return self.tutorialPages.first
            }
        }

        return nil
    }

    func pageViewController(_ pageViewController: UIPageViewController, didFinishAnimating finished: Bool, previousViewControllers: [UIViewController], transitionCompleted completed: Bool) {

        if let viewControllers = pageViewController.viewControllers {
            if let viewControllerIndex = self.tutorialPages.firstIndex(of: viewControllers[0])
            {
                self.pageControl.currentPage = viewControllerIndex
            }
        }
    }


}

问题不在于没有调用委托函数,而在于将
UIPageViewController
完全覆盖在
UIPageControl

您可以通过添加此行来确认这一点:

   // existing line
   self.view.addSubview(self.pageControl)

   // add this line
   self.pageControl.backgroundColor = .orange
按如下方式更改约束设置:

    // completely remove these lines
    //self.pageControl.bottomAnchor.constraint(equalTo: self.view.bottomAnchor, constant: -5).isActive = true
    //self.pageControl.widthAnchor.constraint(equalTo: self.view.widthAnchor, constant: -20).isActive = true
    //self.pageControl.heightAnchor.constraint(equalTo: self.view.heightAnchor, constant: 20).isActive = true
    //self.pageControl.centerXAnchor.constraint(equalTo: self.view.centerXAnchor).isActive = true

    let g = view.safeAreaLayoutGuide

    NSLayoutConstraint.activate([
        pageControl.widthAnchor.constraint(equalTo: g.widthAnchor, constant: -20.0),
        pageControl.centerXAnchor.constraint(equalTo: g.centerXAnchor),
        pageControl.bottomAnchor.constraint(equalTo: g.bottomAnchor, constant: -5.0),
    ])
现在,您应该看到底部的页面控件,您可以在页面中滑动


顺便说一句,
UIPageViewController
有一个“内置的”
UIPageControl
,你可能会发现它工作得更好。

我觉得这只是对我不理解的事情的疏忽!几周前,我刚开始了我的第一份开发工作,参加了为期一周的Swift速成班,完全没有受到程序限制。您的解决方案非常有效,谢谢!!
    // completely remove these lines
    //self.pageControl.bottomAnchor.constraint(equalTo: self.view.bottomAnchor, constant: -5).isActive = true
    //self.pageControl.widthAnchor.constraint(equalTo: self.view.widthAnchor, constant: -20).isActive = true
    //self.pageControl.heightAnchor.constraint(equalTo: self.view.heightAnchor, constant: 20).isActive = true
    //self.pageControl.centerXAnchor.constraint(equalTo: self.view.centerXAnchor).isActive = true

    let g = view.safeAreaLayoutGuide

    NSLayoutConstraint.activate([
        pageControl.widthAnchor.constraint(equalTo: g.widthAnchor, constant: -20.0),
        pageControl.centerXAnchor.constraint(equalTo: g.centerXAnchor),
        pageControl.bottomAnchor.constraint(equalTo: g.bottomAnchor, constant: -5.0),
    ])