[IOS]Presenting modal in iOS 13 fullscreen

参考: https://stackoverflow.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.
wRyeE.pnguploading.4e448015.gif转存失败重新上传取消enter image description here

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错误反而用不了

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值