Presenting modal in iOS 13 fullscreen

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

    an alternative approach is to have your own base viewcontroller component in your app, and just implementing the designated and required initialisers with a basic setup, something like the following:

    class MyBaseViewController: UIViewController {
    
    //MARK: Initialisers
    
    /// Alternative initializer which allows you to set the modal presentation syle
    /// - Parameter modalStyle: the presentation style to be used
    init(with modalStyle:UIModalPresentationStyle) {
        super.init(nibName: nil, bundle: nil)
        self.setup(modalStyle: modalStyle)
    }
    
    override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?) {
        super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
        // default modal presentation style as fullscreen
        self.setup(modalStyle: .fullScreen)
    }
    
    required init?(coder: NSCoder) {
        super.init(coder: coder)
        // default modal presentation style as fullscreen
        self.setup(modalStyle: .fullScreen)
    }
    
    //MARK: Private
    
    /// Setup the view
    ///
    /// - Parameter modalStyle: indicates which modal presentation style to be used
    /// - Parameter modalPresentation: default true, it prevent modally presented view to be dismissible with the default swipe gesture
    private func setup(modalStyle:UIModalPresentationStyle, modalPresentation:Bool = true){
        if #available(iOS 13, *) {
            self.modalPresentationStyle = modalStyle
            self.isModalInPresentation = modalPresentation
        }
    }
    

    NOTE: If your view controller is contained in a navigation controller which is actually presented modally, then the navigation controller should approach the problem in the same way (meaning, having your custom navigation controller component customised in the same way

    Tested on Xcode 11.1 on iOS 13.1 and iOS 12.4

    Hope it helps

提交回复
热议问题