UIPageViewController 中导航栏的更改无法正常工作

Changes on navigation bar in UIPageViewController not working properly

我的代码:

@IBOutlet weak var pageControllerView: UIView!
@IBOutlet weak var pageController: UIPageControl!
// The pages it contains
var pages =  [UIViewController]()

// The UIPageViewController
var pageContainer: UIPageViewController!

viewDodLoad()

 // Setup the pages
    let storyboard = UIStoryboard(name: "Main", bundle: nil)
    page1 = storyboard.instantiateViewController(withIdentifier: "DVC") as? DashboardViewController1
    page2 = storyboard.instantiateViewController(withIdentifier: "MDVC") as? DashboardViewController2
    pages.append(page1)
    pages.append(page2)

    // Create the page container
    pageContainer = UIPageViewController(transitionStyle: .pageCurl, navigationOrientation: .horizontal, options: nil)
    pageContainer.delegate = self
    pageContainer.dataSource = self

    self.pageContainer.setViewControllers([self.page1], direction: UIPageViewController.NavigationDirection.forward, animated: false, completion: nil)

    DispatchQueue.main.async {
        self.firstLabel.text = "Dashboard1"
        self.filterBarButton.isEnabled = true
        self.filterBarButton.tintColor = .white
    }

    // Add it to the view
    pageControllerView.addSubview(pageContainer.view)

    // set the frame of the pageContainer view to match its superview (the pageControllerView view)
    pageContainer.view.frame =  pageControllerView.bounds
    // let it resize if needed (such as device rotation)
    pageContainer.view.autoresizingMask = [.flexibleWidth, .flexibleHeight]

    // Configure our custom pageControl
    // view.bringSubviewToFront(pageController)
    pageController.numberOfPages = pages.count
    pageController.currentPage = 0



// MARK: - UIPageViewController delegates
func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController? {
    let currentIndex = pages.firstIndex(of:viewController)!
    print(currentIndex) //Prints 0
    if currentIndex == 0 {
        DispatchQueue.main.async {
            self.firstLabel.text = "Dashboard1"
            self.filterBarButton.isEnabled = true
            self.filterBarButton.tintColor = .white
        }
        return nil
    }
    let previousIndex = abs((currentIndex - 1) % pages.count)

    return pages[previousIndex]
}

func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController? {
    let currentIndex = pages.firstIndex(of:viewController)!
    print(currentIndex) //Print 1
    if currentIndex == 1 {
        DispatchQueue.main.async {
        self.firstLabel.text = "Dashboard2"
        self.filterBarButton.isEnabled = false
        self.filterBarButton.tintColor = .clear
        }
        return nil
    }
    let nextIndex = abs((currentIndex + 1) % pages.count)
    return pages[nextIndex]
}

func pageViewController(_ pageViewController: UIPageViewController, willTransitionTo pendingViewControllers: [UIViewController]) {
    pendingIndex = pages.firstIndex(of:pendingViewControllers.first!)
}

func pageViewController(_ pageViewController: UIPageViewController, didFinishAnimating finished: Bool, previousViewControllers: [UIViewController], transitionCompleted completed: Bool) {
    if completed {
        currentIndex = pendingIndex
        if let index = currentIndex {
            pageController.currentPage = index
        }
    }
}

这里我想在移动页面时更改导航标签标题和栏按钮状态。有时工作有时不工作。当我们在页面显示后再次向左或向右拖动页面时,现在它正在改变。

实施:

func pageViewController(_ pageViewController: UIPageViewController, didFinishAnimating finished: Bool, previousViewControllers: [UIViewController], transitionCompleted completed: Bool) {
    if completed {
        currentIndex = pendingIndex
        print(currentIndex)
        if currentIndex == 0 {
            DispatchQueue.main.async {
                self.firstLabel.text = "Dashboard1"
                self.filterBarButton.isEnabled = true
                self.filterBarButton.tintColor = .white
            }
        }
        if currentIndex == 1 {
            DispatchQueue.main.async {
                self.firstLabel.text = "Dashboard2"
                self.filterBarButton.isEnabled = false
                self.filterBarButton.tintColor = .clear
            }
        }
        if let index = currentIndex {
            pageController.currentPage = index
        }
    }
}

加载视图时不更改导航栏