如果使用系統(tǒng)IOS12.1 UINavigationController + UITabBarController( UITabBar 磨砂),在popViewControllerAnimated 會(huì)遇到tabbar布局錯(cuò)亂的問題:
- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated{
if (self.childViewControllers.count > 0) {
//如果沒這行代碼芝雪,是正常顯示的
viewController.hidesBottomBarWhenPushed = YES;
}
[super pushViewController:viewController animated:animated];
}
可以使用 QMUI_iOS/issues 提到的解決方案解決:
這個(gè)問題是 iOS 12.1 Beta 2 引入的問題,只要 UITabBar 是磨砂的娩鹉,并且 push viewController 時(shí) hidesBottomBarWhenPushed = YES 則手勢返回的時(shí)候就會(huì)觸發(fā)牙瓢。
出現(xiàn)這個(gè)現(xiàn)象的直接原因是 tabBar 內(nèi)的按鈕 UITabBarButton 被設(shè)置了錯(cuò)誤的 frame酌儒,frame.size 變?yōu)?(0, 0) 導(dǎo)致的笤喳。如果12.1正式版Apple修復(fù)了這個(gè)bug可以移除調(diào)這段代碼(來源于QMUIKit的處理方式),如果12.1正式版本Apple Fix了這個(gè)bug,可以移除掉這個(gè)bug
具體的解決方案是:
@interface CustomTabBar ()
@end
@implementation CustomTabBar
#pragma mark - -----------------以下兩個(gè)方法解決ios12.1tabbar圖標(biāo)位移問題为居,如以后IOS12.1解決則可移除--------------
/**
* 用 block 重寫某個(gè) class 的指定方法
* @param targetClass 要重寫的 class
* @param targetSelector 要重寫的 class 里的實(shí)例方法,注意如果該方法不存在于 targetClass 里杀狡,則什么都不做
* @param implementationBlock 該 block 必須返回一個(gè) block蒙畴,返回的 block 將被當(dāng)成 targetSelector 的新實(shí)現(xiàn),所以要在內(nèi)部自己處理對(duì) super 的調(diào)用捣卤,以及對(duì)當(dāng)前調(diào)用方法的 self 的 class 的保護(hù)判斷(因?yàn)槿绻?targetClass 的 targetSelector 是繼承自父類的忍抽,targetClass 內(nèi)部并沒有重寫這個(gè)方法八孝,則我們這個(gè)函數(shù)最終重寫的其實(shí)是父類的 targetSelector董朝,所以會(huì)產(chǎn)生預(yù)期之外的 class 的影響,例如 targetClass 傳進(jìn)來 UIButton.class干跛,則最終可能會(huì)影響到 UIView.class)子姜,implementationBlock 的參數(shù)里第一個(gè)為你要修改的 class,也即等同于 targetClass,第二個(gè)參數(shù)為你要修改的 selector哥捕,也即等同于 targetSelector牧抽,第三個(gè)參數(shù)是 targetSelector 原本的實(shí)現(xiàn),由于 IMP 可以直接當(dāng)成 C 函數(shù)調(diào)用遥赚,所以可利用它來實(shí)現(xiàn)“調(diào)用 super”的效果扬舒,但由于 targetSelector 的參數(shù)個(gè)數(shù)、參數(shù)類型凫佛、返回值類型讲坎,都會(huì)影響 IMP 的調(diào)用寫法,所以這個(gè)調(diào)用只能由業(yè)務(wù)自己寫愧薛。
*/
CG_INLINE BOOL
OverrideImplementation(Class targetClass, SEL targetSelector, id (^implementationBlock)(Class originClass, SEL originCMD, IMP originIMP)) {
Method originMethod = class_getInstanceMethod(targetClass, targetSelector);
if (!originMethod) {
return NO;
}
IMP originIMP = method_getImplementation(originMethod);
method_setImplementation(originMethod, imp_implementationWithBlock(implementationBlock(targetClass, targetSelector, originIMP)));
return YES;
}
+ (void)load {
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
if (@available(iOS 12.1, *)) {
OverrideImplementation(NSClassFromString(@"UITabBarButton"), @selector(setFrame:), ^id(__unsafe_unretained Class originClass, SEL originCMD, IMP originIMP) {
return ^(UIView *selfObject, CGRect firstArgv) {
if ([selfObject isKindOfClass:originClass]) {
// 如果發(fā)現(xiàn)即將要設(shè)置一個(gè) size 為空的 frame晨炕,則屏蔽掉本次設(shè)置
if (!CGRectIsEmpty(selfObject.frame) && CGRectIsEmpty(firstArgv)) {
return;
}
}
// call super
void (*originSelectorIMP)(id, SEL, CGRect);
originSelectorIMP = (void (*)(id, SEL, CGRect))originIMP;
originSelectorIMP(selfObject, originCMD, firstArgv);
};
});
}
});
}