iOS 13.0 之 presentViewController 模態全屏適配解決方案

在iOS 13.0 之前,模態顯示視圖默認是全屏,但是iOS 13.0 之後,默認是Sheet卡片樣式的非全屏,即:

之前,modalPresentationStyle值默認爲:UIModalPresentationFullScreen;

之後,modalPresentationStyle默認值爲:UIModalPresentationAutomatic;

解決方案:

第一種:在每個方法中添加/修改控制器屬性值modalPresentationStyle爲UIModalPresentationFullScreen即可解決,代碼如下:

-(void)openTypeDetailVC:(int)row{
    ITQuestionDetailViewController *detailVC = [[ITQuestionDetailViewController alloc] init];
    detailVC.modalPresentationStyle = UIModalPresentationFullScreen;
    [self presentViewController:detailVC animated:YES completion:nil];
}

對比效果圖如下:

  

 

第二種:利用OC運行時(Runtime)特性做全局替換修改,免得采用方法一導致遺漏某個頁面,同時也能修改第三方代碼中的模態顯示,如騰訊廣告首頁開屏等,原理就是在運行時檢查方法,然後做IMP交互,讓方法重載,執行自定義代碼,全部代碼如下:

#import <UIKit/UIKit.h>

NS_ASSUME_NONNULL_BEGIN

@interface UIViewController (ITModal)

@end

NS_ASSUME_NONNULL_END
#import "UIViewController+ITModal.h"
#import <objc/runtime.h>

@implementation UIViewController (ITModal)

+ (void)load{
    [super load];
    
    SEL originalSel = @selector(presentViewController:animated:completion:);
    SEL overrideSel = @selector(override_presentViewController:animated:completion:);
    
    Method originalMet = class_getInstanceMethod(self.class, originalSel);
    Method overrideMet = class_getInstanceMethod(self.class, overrideSel);
    
    method_exchangeImplementations(originalMet, overrideMet);
}

#pragma mark - Swizzling
- (void)override_presentViewController:(UIViewController *)viewControllerToPresent animated: (BOOL)flag completion:(void (^ __nullable)(void))completion{
    if(@available(iOS 13.0, *)){
        if (viewControllerToPresent.modalPresentationStyle ==  UIModalPresentationPageSheet){
            viewControllerToPresent.modalPresentationStyle = UIModalPresentationFullScreen;
        }
    }
    
    [self override_presentViewController:viewControllerToPresent animated:flag completion:completion];
}
                                  

@end

只需將本Category類放入工程即可解決。

對比效果圖如下(解決三方平臺無法修改源代碼問題):

 

  

 

至此,iOS 13.0 的模態全屏適配顯示問題得到了比較完美的解決。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章