gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 05:32:54 26 4
gpt4 key购买 nike

我的代码:

@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状态。有时工作有时不工作。当我们在页面显示后再次向左或向右拖动页面时,现在它发生了变化。

最佳答案

实现:

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
}
}
}

并在加载 View 时更改导航栏

关于ios - UIPageViewController 中导航栏的更改无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57356419/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com