tabbar 跳動(dòng)

bug:

iOS 12.1 tabbar 從二級(jí)頁(yè)面返回的時(shí)候,出現(xiàn)跳動(dòng)
iPhone X 下,UITabBarController 在切換界面時(shí) tabBar 往上跳動(dòng)

解決方案:

#pragma mark 解決一個(gè)bug: UITabBar跳動(dòng)的bug(https://github.com/ChenYilong/iOS12AdaptationTips/issues/3)
/**
 *  用 block 重寫(xiě)某個(gè) class 的指定方法
 *  @param targetClass 要重寫(xiě)的 class
 *  @param targetSelector 要重寫(xiě)的 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)部并沒(méi)有重寫(xiě)這個(gè)方法,則我們這個(gè)函數(shù)最終重寫(xiě)的其實(shí)是父類的 targetSelector,所以會(huì)產(chǎn)生預(yù)期之外的 class 的影響,例如 targetClass 傳進(jìn)來(lái)  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)用,所以可利用它來(lái)實(shí)現(xiàn)“調(diào)用 super”的效果,但由于 targetSelector 的參數(shù)個(gè)數(shù)、參數(shù)類型、返回值類型,都會(huì)影響 IMP 的調(diào)用寫(xiě)法,所以這個(gè)調(diào)用只能由業(yè)務(wù)自己寫(xiě)。
 */
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;
}
static CGFloat const kIPhoneXTabbarButtonErrorHeight = 33;
static CGFloat const kIPhoneXTabbarButtonHeight = 48;
+ (void)load {
    /* 這個(gè)問(wèn)題是 iOS 12.1 Beta 2 的問(wèn)題,只要 UITabBar 是磨砂的,并且 push viewController 時(shí) hidesBottomBarWhenPushed = YES 則手勢(shì)返回的時(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)這段代碼(來(lái)源于QMUIKit的處理方式)*/
    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;
                        }
                        //兼容IphoneX
                        if (firstArgv.size.height == kIPhoneXTabbarButtonErrorHeight) {
                            firstArgv.size.height = kIPhoneXTabbarButtonHeight;
                        }
                    }
            
                    
                    // call super
                    void (*originSelectorIMP)(id, SEL, CGRect);
                    originSelectorIMP = (void (*)(id, SEL, CGRect))originIMP;
                    originSelectorIMP(selfObject, originCMD, firstArgv);
                };
            });
        }
    });
}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容