如何将 PageViewController 设置为覆盖整个屏幕而不是模态?
How to set the PageViewController to cover the whole screen and not be modal?
我正在为我的应用程序实现一个 UIPageViewController,以尝试构建一个类似于 Tinder 的 UI,您可以在其中左右滑动,不仅可以喜欢或不喜欢一个人,还可以导航不同的屏幕,即聊天屏幕、个人资料屏幕、比赛屏幕等。
在我的例子中,登录后,将弹出一个包含 4 个其他 UIViewController 的 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]
}
}
默认情况下,当您在 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。
我正在为我的应用程序实现一个 UIPageViewController,以尝试构建一个类似于 Tinder 的 UI,您可以在其中左右滑动,不仅可以喜欢或不喜欢一个人,还可以导航不同的屏幕,即聊天屏幕、个人资料屏幕、比赛屏幕等。
在我的例子中,登录后,将弹出一个包含 4 个其他 UIViewController 的 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]
}
}
默认情况下,当您在 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。