美文网首页
iOS13 present控制器不全屏处理

iOS13 present控制器不全屏处理

作者: 九龙 | 来源:发表于2019-10-25 17:26 被阅读0次
    升级iOS13,发现present模式modalPresentationStyle发生了改变,原来是全屏.fullScreen
    现在新加了.automatic,但手机实测发现模式为.pageSheet

    找到了比较方便的全局修改方法看到了,直接上代码:

    //MARK: - swizzlePresent
    extension UIViewController{
        //主要作用是适配ios13,将present变为全屏
          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)
            }
    
          }
    
          @objc
          private func swizzledPresent(_ viewControllerToPresent: UIViewController,
                                       animated flag: Bool,
                                       completion: (() -> Void)? = nil) {
            if #available(iOS 13.0, *) {
              if viewControllerToPresent.modalPresentationStyle == .automatic || viewControllerToPresent.modalPresentationStyle == .pageSheet{
                viewControllerToPresent.modalPresentationStyle = .fullScreen
              }
            }
            swizzledPresent(viewControllerToPresent, animated: flag, completion: completion)
        }
    }
    
    

    在AppDelegate里面调用一下就好了

    UIViewController.swizzlePresent()
    

    相关文章

      网友评论

          本文标题:iOS13 present控制器不全屏处理

          本文链接:https://www.haomeiwen.com/subject/yfcevctx.html