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

Posted

技术标签:

【中文标题】UIPageViewController 中导航栏的更改无法正常工作【英文标题】:Changes on navigation bar in UIPageViewController not working properly 【发布时间】:2019-12-12 20:22:25 【问题描述】:

我的代码:

@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
        
    

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

【问题讨论】:

【参考方案1】:

实现:

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
        
    

加载视图时不要更改导航栏

【讨论】:

不,您没有在正确的位置更新它,您应该将导航栏更改为委托功能 我这样写可以吗 我从 viewControllerBefore and viewControllerAfter 代表中删除了 if currentIndex == 1 DispatchQueue.main.async self.firstLabel.text = "Dashboard2" self.filterBarButton.isEnabled = false self.filterBarButton.tintColor = .clear 这段代码 现在有了它,它应该可以工作了。对吗? 是的,它工作正常,我从 viewControllerBefore and viewControllerAfter 删除了相同的代码

以上是关于UIPageViewController 中导航栏的更改无法正常工作的主要内容,如果未能解决你的问题,请参考以下文章

如何在uipageviewcontroller swift中转换时隐藏导航栏

导航栏在 UIPageController 下无法正常工作?

以编程方式平滑滚动 UIPageViewController

Swift - UITapGestureRecognizer UIPageViewController

NavigationController中的UIPageViewController

导航后导航栏损坏