欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

[IOS]Presenting modal in iOS 13 fullscreen

程序员文章站 2022-05-30 22:22:10
...

参考: 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.
[IOS]Presenting modal in iOS 13 fullscreen
            
    
    博客分类: IOS presentpage sheetfull screen 

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