[IOS]Presenting modal in iOS 13 fullscreen
参考: https://*.com/questions/56435510/presenting-modal-in-ios-13-fullscreen
There are multiple ways to do that, and I think each one could fit for one project but not another, so I thought I'll keep them here maybe someone else will run to a different case.
1- Override present
If you have a BaseViewController
you can override the present(_ viewControllerToPresent: animated flag: completion:)
method.
classBaseViewController:UIViewController{// ....override func present(_ viewControllerToPresent:UIViewController,
animated flag:Bool,
completion:(()->Void)?=nil){
viewControllerToPresent.modalPresentationStyle =.fullScreen
super.present(viewControllerToPresent, animated: flag, completion: completion)}// ....}
Using this way you don't need to do any change on any present
call, as we just overrode the present
method.
2- An extension:
extension UIViewController{
func presentInFullScreen(_ viewController:UIViewController,
animated:Bool,
completion:(()->Void)?=nil){
viewController.modalPresentationStyle =.fullScreen
present(viewController, animated: animated, completion: completion)}}
Usage:
presentInFullScreen(viewController, animated:true)
3- For one UIViewController
let viewController =UIViewController()
viewController.modalPresentationStyle =.fullScreen
present(viewController, animated:true, completion:nil)
4- From Storyboard
Select a segue and set the presentation to FullScreen
.
5- Swizzling
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)}}@objcprivate 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)}}
Usage:
In your AppDelegate
inside application(_ application: didFinishLaunchingWithOptions)
add this line:
UIViewController.swizzlePresent()
共有5种方法,第一第二种都不错,第三种比较常见,如果很多地方用到present就每一个都需要设置一下,不太优雅.
第四种是要有storyboard的segue,如果代码跳转用不上.
第五种其实是最好的很优雅,而且基本对原代码没有修改.但是如果原代码是OC的话,会有问题,提示selector错误反而用不了
推荐阅读
-
iOS13原生端适配攻略(推荐)
-
iOS 13适配汇总(推荐)
-
iOS13适配的实现方法
-
解决iOS13 无法获取WiFi名称(SSID)问题
-
iOS13即将到来,iOS推送DeviceToken适配方案详解
-
iOS13 适配和Xcode11.0踩坑小结
-
iOS13适配深色模式(Dark Mode)的实现
-
iOS11.3以下modal中input光标错位的解决方法
-
苹果A12/A13设备iOS 13越狱发布:iPhone XS、iPhone 11可玩性大增
-
关于BootStrap modal 在IOS9中不能弹出的解决方法(IOS 9 bootstrap modal ios 9 noticework)