在iOS 13中,出现模态视图控制器时有新行为。
现在默认情况下它不是全屏显示,当我尝试向下滑动时,该应用程序会自动关闭View Controller。
如何防止这种行为并回到旧的全屏模式vc?
谢谢
在iOS 13中,出现模态视图控制器时有新行为。
现在默认情况下它不是全屏显示,当我尝试向下滑动时,该应用程序会自动关闭View Controller。
如何防止这种行为并回到旧的全屏模式vc?
谢谢
Answers:
如WWDC 2019期间平台联盟中所述,使用iOS 13,Apple引入了新的默认卡演示文稿。为了强制全屏,您必须使用以下命令明确指定它:
let vc = UIViewController()
vc.modalPresentationStyle = .fullScreen //or .overFullScreen for transparency
self.present(vc, animated: true, completion: nil)
isModalInPresentation
从驳回阻止滑动手势。请参见我的博客文章了解详情:medium.com/@hacknicity/...
.automatic
样式已定为默认样式,这是(对于大多数视图控制器而言).pageSheet
样式。但是,某些系统视图控制器可能会将其映射为其他样式。
有多种方法可以做到这一点,我认为每个项目都可以适合一个项目,但又不适合另一个项目,所以我认为我会保留在这里,也许其他人会遇到不同的情况。
如果您有一个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)
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()
使用这种方式,您不需要在任何当前调用上进行任何更改,因为我们正在运行时替换当前方法的实现。
如果您想知道什么在泛滥,可以查看以下链接:https :
//nshipster.com/swift-objc-runtime/
我在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
}
static let
是惰性的,您必须阅读它才能进行计算,惰性var仅计算一次,我用它来确保一次调用会
对于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全屏,
overCurrentContext
并带有和navigationController
测试代码
let controller = UIViewController()
let navigationController = UINavigationController(rootViewController: controller)
navigationController.modalPresentationStyle = .overCurrentContext
self.navigationController?.present(navigationController, animated: true, completion: nil)
modalPresentationStyle需要在navigationController处设置。
提示:如果您调用当前存在于ViewController
内嵌在中的NavigationController
,则必须将设置NavigationController
为.fullScreen
而不是VC。
您可以像@davidbates一样执行此操作,也可以以编程方式(例如@pascalbros)来执行。
一个示例场景:
//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)
这是一个简单的解决方案,无需编码一行。
此更改使iPad应用程序的行为符合预期,否则新屏幕将在弹出窗口的中央显示。
这是Objective-C的解决方案
UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
ViewController *vc = [storyBoard instantiateViewControllerWithIdentifier:@"ViewController"];
vc.modalPresentationStyle = UIModalPresentationFullScreen;
[self presentViewController:vc animated:YES completion:nil];
最新适用于iOS 13和Swift 5.x
let vc = ViewController(nibName: "ViewController", bundle: nil)
vc.modalPresentationStyle = .fullScreen
self.present(vc, animated: true, 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的人。这是我的建议:使用Regex。
以下格式不适用于全屏页面:
<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"/>
最初,默认值是fullscreen
modalPresentationStyle,但在iOS 13中,其对UIModalPresentationStyle.automatic
。
如果要制作全屏视图控制器,则必须将更modalPresentationStyle
改为fullScreen
。
请参阅UIModalPresentationStyle
Apple文档以获取更多详细信息,并参阅Apple人机界面指南以了解应在哪里使用哪种模式。
let Obj = MtViewController()
Obj.modalPresentationStyle = .overFullScreen
self.present(Obj, animated: true, completion: nil)
//如果您要禁用滑动以关闭它,请添加行
Obj.isModalInPresentation = true
检查Apple文档以获取更多信息。
我通过使用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中:通过调用以下代码来调用混乱的代码:
UIViewController.setPresentationStyle_fullScreen()
一种替代方法是在您的应用程序中拥有您自己的基本viewcontroller组件,并仅通过基本设置来实现指定的和必需的初始化程序,如下所示:
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上测试
希望能帮助到你
如果您使用UINavigationController并将ViewController嵌入为根视图控制器,那么您也会遇到同样的问题。使用以下代码来克服。
let vc = UIViewController()
let navController = UINavigationController(rootViewController: vc)
navController.modalPresentationStyle = .fullScreen
class MyViewController: UIViewController {
convenience init() {
self.init(nibName:nil, bundle:nil)
self.modalPresentationStyle = .fullScreen
}
override func viewDidLoad() {
super.viewDidLoad()
}
}
self.modalPresentationStyle = .fullScreen
可以为UIViewController子类化,而不必在每个视图控制器上调用,而可以MyViewController
在任何地方使用。
上面的答案和建议是正确的,下面是另一个版本,并且是通过编程有效使用的方式。
#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)
fullScreen
选项应为默认选项,以防止破坏UI更改。