[英]UIPageViewController not calling delegate methods (Swift 5)
我的項目中有一個非常簡單的 UIPageViewController 設置。 當我創建我的頁面視圖控制器並導航到它時,一切似乎都正常工作並且它正確加載了第一頁。 但是,當我嘗試滑動或切換到 ViewControllers 數組中的其他頁面時,我的頁面控制器不會調用其委托方法來執行此操作。
這是我從父視圖實例化和導航到頁面視圖控制器的方式:
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
}
}
}
}
這是我的一個非常簡單的視圖控制器的示例,由頁面視圖控制器顯示:
import UIKit
class TutorialPage1ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = UIColor.black
let label = UILabel()
label.text = "Tutorial page 1"
label.textColor = UIColor.white
self.view.addSubview(label)
label.translatesAutoresizingMaskIntoConstraints = false
label.topAnchor.constraint(equalTo: self.view.topAnchor, constant: 50).isActive = true
label.leftAnchor.constraint(equalTo: self.view.leftAnchor, constant: 20).isActive = true
}
}
問題不在於委托funcs中沒有被調用,問題是,你完全覆蓋你的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
,你可能會發現它的效果更好。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.