iOS12.1 正式版導致的TabBar 裡面item錯誤偏移解決辦法
iOS12.1 使用 UINavigationController + UITabBarController( UITabBar 磨砂),設定hidesBottomBarWhenPushed後,在 pop 後,會引起TabBar佈局異常
如果使用系統OS12.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]; }
經過 @YaoJuan @MoLice 的提示,可以使用ofollow,noindex">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
在這裡有討論:
ChenYilong/CYLTabBarController#312
具體的解決方案是:
需要進行如下設定:
// .h @interface CYLTabBar : UITabBar @end // .m #import "CYLTabBar.h" /** *用 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; } @implementation CYLTabBar + (void)load { /* 這個問題是 iOS 12.1 Beta 2 的問題,只要 UITabBar 是磨砂的,並且 push viewController 時 hidesBottomBarWhenPushed = YES 則手勢返回的時候就會觸發。 出現這個現象的直接原因是 tabBar 內的按鈕 UITabBarButton 被設定了錯誤的 frame,frame.size 變為 (0, 0) 導致的。如果12.1正式版Apple修復了這個bug可以移除調這段程式碼(來源於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]) { // 如果發現即將要設定一個 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); }; }); } }); } @end
目前我已經在我寫的這個庫CYLTabBarController
進行了修改相容。
相關程式碼修改:ChenYilong/CYLTabBarController@2c741c8
更多change log見:https://github.com/ChenYilong/CYLTabBarController/releases