控制器pop返回時,tabbar變形問題

如果使用系統IOS12.1 UINavigationController + UITabBarController( UITabBar 磨砂),在popViewControllerAnimated 會遇到tabbar布局錯亂的問題, 找了好多文章沒有發現解決方案

可以使用 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

具體的解決方案是: 將下段代碼復制粘貼到你的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 的新實現,所以要在內部自己處理對 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;
                        }
                    }
                    
                    // call super
                    void (*originSelectorIMP)(id, SEL, CGRect);
                    originSelectorIMP = (void (*)(id, SEL, CGRect))originIMP;
                    originSelectorIMP(selfObject, originCMD, firstArgv);
                };
            });
        }
    });
}
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容

  • 在與人交往中,留下一個好的印象是開始友誼的第一步。 兩個印象階段 1.第一印象很重要。人們都有先入為主的觀念。2....
    靜心觀情閱讀 163評論 2 2
  • 今年30歲了,人生低潮是從生了孩子以后,當初因為愛情,不顧家人反對,跟了一個一無所有的男人,家里沒有婆婆,婆婆去了...
    秦渝閱讀 721評論 8 9
  • 假期看到了草莓發的橙子圖片,知道橙子快要上架了,心里又開始想了!我懷孕時很喜歡吃橙子,結果兒子也喜歡! ...
    冰糖葫蘆德媽閱讀 244評論 0 0