如何解决如何设置 PageViewController 覆盖整个屏幕而不是模态?
我正在为我的应用程序实施 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。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。