在 iOS 13 中,模态视图控制器在呈现时有一个新的行为。
现在它默认不是全屏的,当我尝试向下滑动时,应用程序会自动关闭视图控制器。
如何防止这种行为并回到旧的全屏模式 vc?
谢谢
在 iOS 13 中,正如WWDC 2019 期间的平台国情所述,Apple 引入了新的默认卡片演示。为了强制全屏,您必须明确指定它:
let vc = UIViewController()
vc.modalPresentationStyle = .fullScreen //or .overFullScreen for transparency
self.present(vc, animated: true, completion: nil)
有多种方法可以做到这一点,我认为每种方法都适合一个项目,但不适用于另一个项目,所以我想我会把它们留在这里,也许其他人会遇到不同的情况。
如果你有一个BaseViewController
你可以覆盖该present(_ viewControllerToPresent: animated flag: completion:)
方法。
class BaseViewController: UIViewController {
// ....
override func present(_ viewControllerToPresent: UIViewController,
animated flag: Bool,
completion: (() -> Void)? = nil) {
viewControllerToPresent.modalPresentationStyle = .fullScreen
super.present(viewControllerToPresent, animated: flag, completion: completion)
}
// ....
}
使用这种方式,您不需要对任何present
调用进行任何更改,因为我们只是覆盖了该present
方法。
extension UIViewController {
func presentInFullScreen(_ viewController: UIViewController,
animated: Bool,
completion: (() -> Void)? = nil) {
viewController.modalPresentationStyle = .fullScreen
present(viewController, animated: animated, completion: completion)
}
}
用法:
presentInFullScreen(viewController, animated: true)
let viewController = UIViewController()
viewController.modalPresentationStyle = .fullScreen
present(viewController, animated: true, completion: nil)
选择一个 segue 并将演示文稿设置为FullScreen
.
extension UIViewController {
static func swizzlePresent() {
let orginalSelector = #selector(present(_: animated: completion:))
let swizzledSelector = #selector(swizzledPresent)
guard let orginalMethod = class_getInstanceMethod(self, orginalSelector), let swizzledMethod = class_getInstanceMethod(self, swizzledSelector) else{return}
let didAddMethod = class_addMethod(self,
orginalSelector,
method_getImplementation(swizzledMethod),
method_getTypeEncoding(swizzledMethod))
if didAddMethod {
class_replaceMethod(self,
swizzledSelector,
method_getImplementation(orginalMethod),
method_getTypeEncoding(orginalMethod))
} else {
method_exchangeImplementations(orginalMethod, swizzledMethod)
}
}
@objc
private func swizzledPresent(_ viewControllerToPresent: UIViewController,
animated flag: Bool,
completion: (() -> Void)? = nil) {
if #available(iOS 13.0, *) {
if viewControllerToPresent.modalPresentationStyle == .automatic {
viewControllerToPresent.modalPresentationStyle = .fullScreen
}
}
swizzledPresent(viewControllerToPresent, animated: flag, completion: completion)
}
}
用法:
在你的AppDelegate
里面application(_ application: didFinishLaunchingWithOptions)
添加这一行:
UIViewController.swizzlePresent()
使用这种方式,您不需要对任何当前调用进行任何更改,因为我们正在运行时替换当前方法实现。
如果您需要知道什么是 swizzling,您可以查看此链接:
https ://nshipster.com/swift-objc-runtime/
对于 Objective-C 用户
只需使用此代码
[vc setModalPresentationStyle: UIModalPresentationFullScreen];
或者,如果您想在 iOS 13.0 中特别添加它,请使用
if (@available(iOS 13.0, *)) {
[vc setModalPresentationStyle: UIModalPresentationFullScreen];
} else {
// Fallback on earlier versions
}
一个班轮:
modalPresentationStyle
需要在正在呈现的navigationController 上进行设置。
iOS 13 及以下 iOS 版本 fullScreen with
overCurrentContext
andnavigationController
测试代码
let controller = UIViewController()
let navigationController = UINavigationController(rootViewController: controller)
navigationController.modalPresentationStyle = .overCurrentContext
self.navigationController?.present(navigationController, animated: true, completion: nil)
modalPresentationStyle需要设置在navigationController。
作为提示:如果您调用 present 到ViewController
嵌入在 a 中的 aNavigationController
您必须设置NavigationController
to.fullScreen
而不是 VC。
您可以像 @davidbates 一样执行此操作,也可以以编程方式执行此操作(例如 @pascalbros)。
这同样适用于UITabViewController
一个示例场景NavigationController
:
//BaseNavigationController: UINavigationController {}
let baseNavigationController = storyboard!.instantiateViewController(withIdentifier: "BaseNavigationController")
var navigationController = UINavigationController(rootViewController: baseNavigationController)
navigationController.modalPresentationStyle = .fullScreen
navigationController.topViewController as? LoginViewController
self.present(navigationViewController, animated: true, completion: nil)
我在 ios 13 上使用了 swizzling
import Foundation
import UIKit
private func _swizzling(forClass: AnyClass, originalSelector: Selector, swizzledSelector: Selector) {
if let originalMethod = class_getInstanceMethod(forClass, originalSelector),
let swizzledMethod = class_getInstanceMethod(forClass, swizzledSelector) {
method_exchangeImplementations(originalMethod, swizzledMethod)
}
}
extension UIViewController {
static let preventPageSheetPresentation: Void = {
if #available(iOS 13, *) {
_swizzling(forClass: UIViewController.self,
originalSelector: #selector(present(_: animated: completion:)),
swizzledSelector: #selector(_swizzledPresent(_: animated: completion:)))
}
}()
@available(iOS 13.0, *)
@objc private func _swizzledPresent(_ viewControllerToPresent: UIViewController,
animated flag: Bool,
completion: (() -> Void)? = nil) {
if viewControllerToPresent.modalPresentationStyle == .pageSheet
|| viewControllerToPresent.modalPresentationStyle == .automatic {
viewControllerToPresent.modalPresentationStyle = .fullScreen
}
_swizzledPresent(viewControllerToPresent, animated: flag, completion: completion)
}
}
然后把这个
UIViewController.preventPageSheetPresentation
某处
例如在 AppDelegate
func application(_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]?) -> Bool {
UIViewController.preventPageSheetPresentation
// ...
return true
}
这对我有用
let vc = self.storyboard?.instantiateViewController(withIdentifier: "storyboardID_cameraview1") as! CameraViewController
vc.modalPresentationStyle = .fullScreen
self.present(vc, animated: true, completion: nil)`
iOS 13 和 Swift 5.x 的最新版本
let vc = ViewController(nibName: "ViewController", bundle: nil)
vc.modalPresentationStyle = .fullScreen
self.present(vc, animated: true, completion: nil)
这是Objective-C的解决方案
UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
ViewController *vc = [storyBoard instantiateViewControllerWithIdentifier:@"ViewController"];
vc.modalPresentationStyle = UIModalPresentationFullScreen;
[self presentViewController:vc animated:YES completion:nil];
这是我在 ObjectiveC 中使用类别的修复版本。使用这种方法,您将拥有默认的 UIModalPresentationStyleFullScreen 行为,直到另一个明确设置。
#import "UIViewController+Presentation.h"
#import "objc/runtime.h"
@implementation UIViewController (Presentation)
- (void)setModalPresentationStyle:(UIModalPresentationStyle)modalPresentationStyle {
[self setPrivateModalPresentationStyle:modalPresentationStyle];
}
-(UIModalPresentationStyle)modalPresentationStyle {
UIModalPresentationStyle style = [self privateModalPresentationStyle];
if (style == NSNotFound) {
return UIModalPresentationFullScreen;
}
return style;
}
- (void)setPrivateModalPresentationStyle:(UIModalPresentationStyle)modalPresentationStyle {
NSNumber *styleNumber = [NSNumber numberWithInteger:modalPresentationStyle];
objc_setAssociatedObject(self, @selector(privateModalPresentationStyle), styleNumber, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
- (UIModalPresentationStyle)privateModalPresentationStyle {
NSNumber *styleNumber = objc_getAssociatedObject(self, @selector(privateModalPresentationStyle));
if (styleNumber == nil) {
return NSNotFound;
}
return styleNumber.integerValue;
}
@end
所有其他答案都足够了,但是对于像我们这样的大型项目以及在代码和情节提要中进行导航的大型项目,这是一项艰巨的任务。
对于那些积极使用 Storyboard 的人。这是我的建议:使用正则表达式。
以下格式不适用于全屏页面:
<segue destination="Bof-iQ-svK" kind="presentation" identifier="importSystem" modalPresentationStyle="fullScreen" id="bfy-FP-mlc"/>
以下格式适用于全屏页面:
<segue destination="7DQ-Kj-yFD" kind="presentation" identifier="defaultLandingToSystemInfo" modalPresentationStyle="fullScreen" id="Mjn-t2-yxe"/>
以下与 VS CODE 兼容的正则表达式会将所有旧样式页面转换为新样式页面。如果您使用其他正则表达式引擎/文本编辑器,您可能需要转义特殊字符。
搜索正则表达式
<segue destination="(.*)"\s* kind="show" identifier="(.*)" id="(.*)"/>
替换正则表达式
<segue destination="$1" kind="presentation" identifier="$2" modalPresentationStyle="fullScreen" id="$3"/>
let Obj = MtViewController()
Obj.modalPresentationStyle = .overFullScreen
self.present(Obj, animated: true, completion: nil)
// 如果你想禁用滑动关闭它,添加行
Obj.isModalInPresentation = true
查看Apple 文档以获取更多信息。
你可以很容易地这样做打开你的故事板作为源代码并搜索kind="presentation"
,在所有带有 kind=presentation 的 seague 标签中添加一个额外的属性modalPresentationStyle="fullScreen"
我通过使用方法 swizzling(Swift 4.2) 实现了它:
如下创建一个 UIViewController 扩展
extension UIViewController {
@objc private func swizzled_presentstyle(_ viewControllerToPresent: UIViewController, animated: Bool, completion: (() -> Void)?) {
if #available(iOS 13.0, *) {
if viewControllerToPresent.modalPresentationStyle == .automatic || viewControllerToPresent.modalPresentationStyle == .pageSheet {
viewControllerToPresent.modalPresentationStyle = .fullScreen
}
}
self.swizzled_presentstyle(viewControllerToPresent, animated: animated, completion: completion)
}
static func setPresentationStyle_fullScreen() {
let instance: UIViewController = UIViewController()
let aClass: AnyClass! = object_getClass(instance)
let originalSelector = #selector(UIViewController.present(_:animated:completion:))
let swizzledSelector = #selector(UIViewController.swizzled_presentstyle(_:animated:completion:))
let originalMethod = class_getInstanceMethod(aClass, originalSelector)
let swizzledMethod = class_getInstanceMethod(aClass, swizzledSelector)
if let originalMethod = originalMethod, let swizzledMethod = swizzledMethod {
method_exchangeImplementations(originalMethod, swizzledMethod)
}
}
}
在 AppDelegate 中,在 application:didFinishLaunchingWithOptions: 中调用 swizzling 代码:
UIViewController.setPresentationStyle_fullScreen()
设置navigationController.modalPresentationStyle
to.fullScreen
已经在这里重复了一千多次,但让我向您展示另一个导致UIViewController
/UINavigationController
未显示的阻止程序,fullscreen
即使所有属性都已正确设置。
就我而言,罪魁祸首隐藏在这一行
navigationController?.presentationController?.delegate = self
显然,在设置时,UIAdaptivePresentationControllerDelegate
您需要在可选委托方法中指定演示样式
public func adaptivePresentationStyle(for controller: UIPresentationController) -> UIModalPresentationStyle {
presentationStyle
}
为 UIViewController 创建一个类别(比如 UIViewController+PresentationStyle)。将以下代码添加到其中。
-(UIModalPresentationStyle)modalPresentationStyle{
return UIModalPresentationStyleFullScreen;
}
另一种方法是在您的应用程序中拥有自己的基本视图控制器组件,并使用基本设置实现指定和必需的初始化程序,如下所示:
class MyBaseViewController: UIViewController {
//MARK: Initialisers
/// Alternative initializer which allows you to set the modal presentation syle
/// - Parameter modalStyle: the presentation style to be used
init(with modalStyle:UIModalPresentationStyle) {
super.init(nibName: nil, bundle: nil)
self.setup(modalStyle: modalStyle)
}
override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?) {
super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
// default modal presentation style as fullscreen
self.setup(modalStyle: .fullScreen)
}
required init?(coder: NSCoder) {
super.init(coder: coder)
// default modal presentation style as fullscreen
self.setup(modalStyle: .fullScreen)
}
//MARK: Private
/// Setup the view
///
/// - Parameter modalStyle: indicates which modal presentation style to be used
/// - Parameter modalPresentation: default true, it prevent modally presented view to be dismissible with the default swipe gesture
private func setup(modalStyle:UIModalPresentationStyle, modalPresentation:Bool = true){
if #available(iOS 13, *) {
self.modalPresentationStyle = modalStyle
self.isModalInPresentation = modalPresentation
}
}
注意:如果您的视图控制器包含在实际以模态方式呈现的导航控制器中,则导航控制器应该以相同的方式解决问题(意味着以相同的方式定制您的自定义导航控制器组件
在 iOS 13.1 和 iOS 12.4 上的 Xcode 11.1 上测试
希望能帮助到你
上面的答案和建议是正确的,下面是另一个版本,并且以编程方式使用的有效方式。
#1 创建了一个 UIView 扩展
#2 创建了一个方法 ()
//#1
extension UIViewController {
//#2
func presentLocal(_ viewControllerToPresent: UIViewController, animated flag:
Bool, completion: (() -> Void)? = nil) {
//Reusing below 2 lines :-)
viewControllerToPresent.modalPresentationStyle = .overCurrentContext
self.present(viewControllerToPresent, animated: flag, completion: completion)
}
}
调用如下
let vc = MyViewController()
let nc = UINavigationController(rootViewController: vc)
sourceView.presentLocal(nc, animated: true, completion: nil)
或者
let vc = MyViewController()
sourceView.presentLocal(vc, animated: true, completion: nil)
class MyViewController: UIViewController {
convenience init() {
self.init(nibName:nil, bundle:nil)
self.modalPresentationStyle = .fullScreen
}
override func viewDidLoad() {
super.viewDidLoad()
}
}
您可以继承 UIViewController 并在任何地方使用,而不是调用self.modalPresentationStyle = .fullScreen
每个视图控制器MyViewController
。