【问题标题】:How to set the PageViewController to cover the whole screen and not be modal?如何设置 PageViewController 覆盖整个屏幕而不是模态?
【发布时间】:2021-07-12 08:54:00
【问题描述】:

我正在为我的应用程序实现一个 UIPageViewController 来尝试构建一个类似 Tinder 的 UI,您可以在其中左右滑动来不仅喜欢或不喜欢一个人,还可以导航不同的屏幕,即聊天屏幕、个人资料屏幕、匹配屏幕等。

在我的例子中,登录后会弹出一个包含 4 个其他 UIViewControllers 的 UIPageViewController。

但是,UIPageViewController 是模态的,不会覆盖整个屏幕(因为顶部有一个小间隙,允许用户向下滑动模态)。 我尝试使用这样的代码:

self.window = UIWindow(frame: UIScreen.main.bounds)
    if let window = self.window {
        window.rootViewController = PageViewController()
        window.makeKeyAndVisible()
    }

在我的 AppDelegate 中,或在情节提要中设置全屏,但没有用。

我想知道我应该怎么做? 或者也许 UIPageViewController 不是 Tinder 实现这种从屏幕到屏幕的滑动导航样式的正确选择?

无论如何,这是我的 PageViewController 的代码:

import UIKit

class PageViewController: UIPageViewController, UIPageViewControllerDataSource, UIPageViewControllerDelegate {
   
    var controllers = [UIViewController]()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        let vc = TodayPicksViewController()
        controllers.append(vc)
        
        let vc1 = TopPicksViewController()
        vc1.view.backgroundColor = .yellow
        controllers.append(vc1)
        
        let vc2 = ChatViewController()
        vc2.view.backgroundColor = .gray
        controllers.append(vc2)
        
        let vc3 = (storyboard?.instantiateViewController(withIdentifier: String(describing: ProfileViewController.self)) as? ProfileViewController)!
        controllers.append(vc3)
        
    }
        
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        
        DispatchQueue.main.asyncAfter(deadline: .now()+2, execute: {
            self.presentPageVC()
        })
    }
    
    func presentPageVC() {
        guard let first = controllers.first else {
            return
        }
        
        let vc = UIPageViewController(transitionStyle: .scroll,
                                      navigationOrientation: .horizontal,
                                      options: nil)
        
        vc.delegate = self
        vc.dataSource = self
        vc.setViewControllers([first],
                              direction: .forward,
                              animated: true,
                              completion: nil)
        present(vc, animated: true)
    }
    
    func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController? {
        guard let index = controllers.firstIndex(of: viewController), index > 0 else {
            return nil
        }
        
        let before = index - 1
        
        return controllers[before]
    }
    
    func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController? {
        guard let index = controllers.firstIndex(of: viewController), index < (controllers.count - 1) else {
            return nil
        }
        
        let after = index + 1
        
        return controllers[after]
    }
}

【问题讨论】:

  • 在调用present(vc, animated: true) 之前,在vc 上设置模态演示样式,您可以这样做vc.modalPresentationStyle = .fullScreen
  • @Andrew 它有效!谢谢!你应该已经在下面回答了,所以我可以选择你作为接受的答案!

标签: swift uipageviewcontroller


【解决方案1】:

默认情况下,当您在 Swift 中呈现 ViewController 时,它不会覆盖全屏。要使其覆盖全屏,您需要在 ViewController 上设置 modalPresentationStyle

因此,在您的 presentPageVC 方法中,您需要添加以下行:

vc.modalPresentationStyle = .fullScreen

所以你的方法现在看起来像这样:

func presentPageVC() {
    guard let first = controllers.first else {
        return
    }
    
    let vc = UIPageViewController(transitionStyle: .scroll,
                                    navigationOrientation: .horizontal,
                                    options: nil)
    
    vc.delegate = self
    vc.dataSource = self
    vc.setViewControllers([first],
                            direction: .forward,
                            animated: true,
                            completion: nil)

    vc.modalPresentationStyle = .fullScreen // <- add this before presenting your ViewController

    present(vc, animated: true)
}

要了解更多关于不同演示风格的信息,请查看文档here

【讨论】:

  • 再次感谢您!
猜你喜欢
  • 2018-07-20
  • 1970-01-01
  • 2017-12-13
  • 1970-01-01
  • 2021-02-09
  • 1970-01-01
  • 1970-01-01
  • 2016-08-22
  • 1970-01-01
相关资源
最近更新 更多