如果使用系統(tǒng)IOS12.1 UINavigationController + UITabBarController( UITabBar 磨砂)恨课,在popViewControllerAnimated 會遇到tabbar布局錯亂的問題, 找了好多文章沒有發(fā)現(xiàn)解決方案
可以使用 QMUI_iOS/issues 提到的解決方案解決:
這個問題是 iOS 12.1 Beta 2 引入的問題呢灶,只要 UITabBar 是磨砂的,并且 push viewController 時 hidesBottomBarWhenPushed = YES 則手勢返回的時候就會觸發(fā)见芹。
出現(xiàn)這個現(xiàn)象的直接原因是 tabBar 內(nèi)的按鈕 UITabBarButton 被設(shè)置了錯誤的 frame剂娄,frame.size 變?yōu)?(0, 0) 導致的。如果12.1正式版Apple修復了這個bug可以移除調(diào)這段代碼(來源于QMUIKit的處理方式),如果12.1正式版本Apple Fix了這個bug,可以移除掉這個bug
具體的解決方案是: 將下段代碼復制粘貼到你的RootTabBar中
@interface RootTabBarController ()
@end
@implementation RootTabBarController
#pragma mark - -----------------以下兩個方法解決ios12.1tabbar圖標位移問題玄呛,如以后IOS12.1解決則可移除--------------
/**
* 用 block 重寫某個 class 的指定方法
* @param targetClass 要重寫的 class
* @param targetSelector 要重寫的 class 里的實例方法阅懦,注意如果該方法不存在于 targetClass 里,則什么都不做
* @param implementationBlock 該 block 必須返回一個 block徘铝,返回的 block 將被當成 targetSelector 的新實現(xiàn)耳胎,所以要在內(nèi)部自己處理對 super 的調(diào)用,以及對當前調(diào)用方法的 self 的 class 的保護判斷(因為如果 targetClass 的 targetSelector 是繼承自父類的惕它,targetClass 內(nèi)部并沒有重寫這個方法怕午,則我們這個函數(shù)最終重寫的其實是父類的 targetSelector,所以會產(chǎn)生預期之外的 class 的影響淹魄,例如 targetClass 傳進來 UIButton.class郁惜,則最終可能會影響到 UIView.class),implementationBlock 的參數(shù)里第一個為你要修改的 class甲锡,也即等同于 targetClass兆蕉,第二個參數(shù)為你要修改的 selector,也即等同于 targetSelector缤沦,第三個參數(shù)是 targetSelector 原本的實現(xiàn)虎韵,由于 IMP 可以直接當成 C 函數(shù)調(diào)用,所以可利用它來實現(xiàn)“調(diào)用 super”的效果缸废,但由于 targetSelector 的參數(shù)個數(shù)包蓝、參數(shù)類型、返回值類型企量,都會影響 IMP 的調(diào)用寫法测萎,所以這個調(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è)置一個 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);
};
});
}
});
}