出現(xiàn)這個(gè)問(wèn)題主要因?yàn)?iOS以后,模態(tài)出來(lái)的viewcontroller中?modalPresentationStyle的默認(rèn)值改變了测萎,
iOS 13之前,modalPresentationStyle值默認(rèn)為:UIModalPresentationFullScreen
在iOS 13中梁钾,modalPresentationStyle默認(rèn)值為:UIModalPresentationAutomatic
修改要模態(tài)viewcontroller的modalPresentationStyle為:UIModalPresentationFullScreen 就就可以填充全屏
后果:被懟了很慘绳泉,因?yàn)轫?xiàng)目里有很多模態(tài)出來(lái)的VC是半透明,結(jié)果現(xiàn)在變成完全不透明姆泻,背景為黑色零酪。
解決方法1:
挨個(gè)修改VC 的modalPresentationStyle冒嫡;代碼多不說(shuō),漏改是大事四苇,萬(wàn)一以后還有很多是半透密的孝凌,每次都要寫(xiě),煩…………
解決方法2:
利用OC的runtime機(jī)制月腋,交換系統(tǒng)方法:presentViewController:animated:completion:
具體實(shí)現(xiàn)如下:
在基類的.m中 做交換
#import <objc/runtime.h>
+ (void)load{
? ? // 適配ios? 13? 這里做方法交換
? ? static?dispatch_once_tonceToken;
? ? dispatch_once(&onceToken, ^{
? ? ? ? // 系統(tǒng)模態(tài)方法蟀架,? 做方法交換
? ? ? ? Method systemPresent =class_getInstanceMethod(self,@selector(presentViewController:animated:completion:));
? ? ? ? Method custom_Method =class_getInstanceMethod(self,@selector(xbPresentViewController:animated:completion:));
? ? ? ? method_exchangeImplementations(systemPresent, custom_Method);
? ? });
}
- (void)xbPresentViewController:(UIViewController*)viewControllerToPresentanimated:(BOOL)flag completion:(void(^)(void))completion {
? ? //設(shè)置滿屏,可以半透明
? ? if(@available(iOS13.0, *)) {
? ? ? ? viewControllerToPresent.modalPresentationStyle = UIModalPresentationOverFullScreen;
? ? }
? ? [self?xbPresentViewController:viewControllerToPresentanimated:flagcompletion:completion];
}
后遺癥:因?yàn)榘扬@示效果修改為:UIModalPresentationOverFullScreen榆骚;半透明片拍,全屏覆蓋的問(wèn)題都得到完美解決。但是會(huì)引發(fā)一個(gè)新的問(wèn)題:前一個(gè)頁(yè)面的viewWillAppear:妓肢、viewDidAppear:也無(wú)法觸發(fā)捌省。例如: A? ?Present? B, 有時(shí)因?yàn)闃I(yè)務(wù)邏輯需要,必須在viewWillAppear碉钠,?viewDidAppear里寫(xiě)一些代碼纲缓,當(dāng)B 調(diào)用dismiss方法的時(shí)候, A的這個(gè)兩個(gè)方法不會(huì)觸發(fā)喊废,因此會(huì)有一些安全隱患祝高。因此如果要求B 調(diào)用dismiss方法,A要執(zhí)行viewWillAppear:污筷、viewDidAppear:這個(gè)兩個(gè)方法工闺,這個(gè)時(shí)候要把B的modalPresentationStyle設(shè)置為:UIModalPresentationFullScreen;
方法改進(jìn):
- (void)myPresentViewController:(UIViewController*)viewControllerToPresentanimated:(BOOL)flagcompletion:(void(^)(void))completion {
? ? //設(shè)置滿屏颓屑,可以半透明
? ? if(@available(iOS13.0, *)) {
? ? ? ? if([viewControllerToPresent isKindOfClass:[YourViewController class]]) {
? ? ? ? ? ? viewControllerToPresent.modalPresentationStyle = UIModalPresentationFullScreen;
? ? ? ? } else {
? ? ? ? ? ? viewControllerToPresent.modalPresentationStyle = UIModalPresentationOverFullScreen;
? ? ? ? }
? ? }
? ? [self myPresentViewController:viewControllerToPresentanimated:flagcompletion:completion];
}
其他:如果要求 B既要半透明斤寂,dismiss時(shí),A還要調(diào)用viewWillAppear:揪惦、viewDidAppear:。我的想法是在B 寫(xiě)一個(gè)block罗侯,在B調(diào)用dismiss之前器腋,利用block回調(diào)A相關(guān)的業(yè)務(wù)邏輯代碼。如果有其他更好的方法請(qǐng)告訴我钩杰。萬(wàn)分感謝H宜!