Presenting modal in iOS 13 fullscreen

后端 未结 30 2078
囚心锁ツ
囚心锁ツ 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:06

    I used swizzling for ios 13

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

    then put this

    UIViewController.preventPageSheetPresentation
    

    somewhere

    for example in AppDelegate

    func application(_ application: UIApplication,
                     didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]?) -> Bool {
    
        UIViewController.preventPageSheetPresentation
        // ...
        return true
    }
    

提交回复
热议问题