Presenting modal in iOS 13 fullscreen

后端 未结 30 2089
囚心锁ツ
囚心锁ツ 2020-11-21 06:48

In iOS 13 there is a new behaviour for modal view controller when being presented.

Now it\'s not fullscreen by default and when I try to slide down, the app just dis

30条回答
  •  太阳男子
    2020-11-21 07:14

    I achieved it by using method swizzling(Swift 4.2):

    To create an UIViewController extension as follows

    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)
            }
        }
    }
    

    and in AppDelegate, in application:didFinishLaunchingWithOptions: invoke the swizzling code by calling:

    UIViewController.setPresentationStyle_fullScreen()
    

提交回复
热议问题