洞察探索如何通过一套代码实现跨平台小程序开发与高效管理,助力企业数字化转型
809
2022-10-08
[IOS]Presenting modal in iOS 13 fullscreen
参考: 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 =.fullScreenpresent(viewController, animated:true, completion:nil)
4- From Storyboard
Select a segue and set the presentation to FullScreen.
转存失败重新上传取消
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错误反而用不了
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~