天天看點

iOS之iOS 12.1 Tabbar從二級頁面傳回跳動問題解決方法(使用iPhone全系列包含iPhone X哦)

如果使用系統IOS12.1 UINavigationController + UITabBarController( UITabBar 磨砂),在popViewControllerAnimated 會遇到tabbar布局錯亂的問題:

- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated{
   
   if (self.childViewControllers.count > 0) {
       //如果沒這行代碼,是正常顯示的
       viewController.hidesBottomBarWhenPushed = YES;
   }
   
   [super pushViewController:viewController animated:animated];
}           

可以使用 QMUI_iOS/issues提到的解決方案解決:

這個問題是 iOS 12.1 Beta 2 引入的問題,隻要 UITabBar 是磨砂的,并且 push viewController 時 hidesBottomBarWhenPushed = YES 則手勢傳回的時候就會觸發。

出現這個現象的直接原因是 tabBar 内的按鈕 UITabBarButton 被設定了錯誤的 frame,frame.size 變為 (0, 0) 導緻的。如果12.1正式版Apple修複了這個bug可以移除調這段代碼(來源于QMUIKit的處理方式),如果12.1正式版本Apple Fix了這個bug,可以移除掉這個bug

具體的解決方案是:

@interface MyCustomTabBar ()

@end

@implementation MyCustomTabBar

#pragma mark -  -----------------以下兩個方法解決ios12.1tabbar圖示位移問題,如以後IOS12.1解決則可移除--------------

/**
 *  用 block 重寫某個 class 的指定方法
 *  @param targetClass 要重寫的 class
 *  @param targetSelector 要重寫的 class 裡的執行個體方法,注意如果該方法不存在于 targetClass 裡,則什麼都不做
 *  @param implementationBlock 該 block 必須傳回一個 block,傳回的 block 将被當成 targetSelector 的新實作,是以要在内部自己處理對 super 的調用,以及對目前調用方法的 self 的 class 的保護判斷(因為如果 targetClass 的 targetSelector 是繼承自父類的,targetClass 内部并沒有重寫這個方法,則我們這個函數最終重寫的其實是父類的 targetSelector,是以會産生預期之外的 class 的影響,例如 targetClass 傳進來  UIButton.class,則最終可能會影響到 UIView.class),implementationBlock 的參數裡第一個為你要修改的 class,也即等同于 targetClass,第二個參數為你要修改的 selector,也即等同于 targetSelector,第三個參數是 targetSelector 原本的實作,由于 IMP 可以直接當成 C 函數調用,是以可利用它來實作“調用 super”的效果,但由于 targetSelector 的參數個數、參數類型、傳回值類型,都會影響 IMP 的調用寫法,是以這個調用隻能由業務自己寫。
 */
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]) {
                        // 如果發現即将要設定一個 size 為空的 frame,則屏蔽掉本次設定
                        if (!CGRectIsEmpty(selfObject.frame) && CGRectIsEmpty(firstArgv)) {
                            return;
                        }
                        if (IS_IPHONEX) {
                            if (!CGRectIsEmpty(selfObject.frame) && (firstArgv.size.height == 33)) {
                                // call super
                                return;
                            }
                        }
                    }
                    
                    // call super
                    void (*originSelectorIMP)(id, SEL, CGRect);
                    originSelectorIMP = (void (*)(id, SEL, CGRect))originIMP;
                    originSelectorIMP(selfObject, originCMD, firstArgv);
                };
            });
        }
    });
}