iOS開(kāi)發(fā)--iOS 12.1系統(tǒng)tabBar按鈕偏移錯(cuò)位問(wèn)題

現(xiàn)在我們使用的APP框架,大部分都是 UINavigationController + UITabBarController的模式。如果使用的是默認(rèn)的UITabBar磨砂效果,而且在pushViewController時(shí),設(shè)置了hidesBottomBarWhenPushed屬性為true ,則在iOS 12.1系統(tǒng),使用手勢(shì)返回的時(shí)候,就會(huì)觸發(fā)UITabBarButton 被設(shè)置frame.size 為 (0, 0)的錯(cuò)誤布局, 導(dǎo)致的tabBar上的圖標(biāo)和文字偏移錯(cuò)位。

案例
extension UINavigationController {
    @objc open func ck_pushViewController(_ viewController: UIViewController, animated: Bool) {
        viewController.hidesBottomBarWhenPushed = true
        self.ck_pushViewController(viewController, animated: animated)
        if self.viewControllers.count == 1 {
            viewController.hidesBottomBarWhenPushed = false
        }
    }
}

方案一:修改tabBarisTranslucent透明度(不推薦)或添加背景圖片

1、可以通過(guò)修改tabBar的透明度達(dá)到我們想要的效果:

if #available(iOS 12.1, *) {
  UITabBar.appearance().isTranslucent = false
} else {
  UITabBar.appearance().isTranslucent = true
}

但是這個(gè)會(huì)引入其它的問(wèn)題:

  • (1)tabBar想保留原來(lái)的磨砂效果;
  • (2)會(huì)影響tableView的布局,底部間距多了tabBar的高度;

所以這種方式pass

2、可以統(tǒng)一給tabBar添加背景圖片,則不會(huì)有這個(gè)問(wèn)題。

方案二:使用runtime過(guò)濾UITabBarButtonframe設(shè)置(推薦)

為了達(dá)到既不修改原先的設(shè)計(jì)又達(dá)到解決這個(gè)問(wèn)題,我這邊通過(guò)交換UITabBarButtonsetFrame方法,過(guò)濾一些錯(cuò)誤的大小設(shè)置,來(lái)達(dá)到我們的目的,這里提供了OCSwift兩個(gè)版本的代碼,如下:

  • OC

添加UIView的分類,并在+load方法中,交換UITabBarButtonsetFrame方法,過(guò)濾錯(cuò)誤的大小

#import <UIKit/UIKit.h>

@interface UIView (CKTabBarItem)

@end
#import "UIView+CKTabBarItem.h"
#import <objc/runtime.h>

@implementation UIView (CKTabBarItem)

+ (void)load
{
    if (@available(iOS 12.1, *)) {
        static dispatch_once_t onceToken;
        dispatch_once(&onceToken, ^{
            Class class = NSClassFromString(@"UITabBarButton");
            SEL originalSelector  = @selector(setFrame:);
            SEL swizzledSelector  = @selector(ck_setFrame:);
            Method originalMethod = class_getInstanceMethod(class, originalSelector);
            Method swizzledMethod = class_getInstanceMethod(class, swizzledSelector);
            BOOL isSuccess = class_addMethod(class,
                                             originalSelector,
                                             method_getImplementation(swizzledMethod),
                                             method_getTypeEncoding(swizzledMethod));
            if (isSuccess) {
                class_replaceMethod(class,
                                    swizzledSelector,
                                    method_getImplementation(originalMethod),
                                    method_getTypeEncoding(originalMethod));
            } else {
                method_exchangeImplementations(originalMethod, swizzledMethod);
            }
        });
    }
}

- (void)ck_setFrame:(CGRect)frame
{
    if (!CGRectIsEmpty(self.frame)) {
        if (CGRectIsEmpty(frame)) {
            return;
        }
        frame.size.height = MAX(frame.size.height, 48.0);
    }
    [self ck_setFrame: frame];
}

@end
  • Swift

Swift4 不支持dispatch_once,靜態(tài)變量默認(rèn)用dispatch_once初始化,可以替代dispatch_once的實(shí)現(xiàn)

import UIKit

extension UIView {
    
    /// Swift4 不支持dispatch_once,靜態(tài)變量默認(rèn)用dispatch_once初始化,可以替代dispatch_once的實(shí)現(xiàn)
    private static let swizzlingTabBarButtonFrame: Void = {
        guard #available(iOS 12.1, *) else {
            return
        }
        guard let cls = NSClassFromString("UITabBarButton") else {
            return
        }
        let originalSelector = #selector(setter: UIView.frame)
        let swizzledSelector = #selector(UIView.ck_setFrame)
        guard let originalMethod = class_getInstanceMethod(cls, originalSelector) else {
            return
        }
        guard let swizzledMethod = class_getInstanceMethod(cls, swizzledSelector) else {
            return
        }
        let isSuccess = class_addMethod(cls,
                                        originalSelector,
                                        method_getImplementation(swizzledMethod),
                                        method_getTypeEncoding(swizzledMethod))
        if (isSuccess) {
            class_replaceMethod(cls,
                                swizzledSelector,
                                method_getImplementation(originalMethod),
                                method_getTypeEncoding(originalMethod));
        } else {
            method_exchangeImplementations(originalMethod, swizzledMethod);
        }
    }()
    
    @objc func ck_setFrame(frame: CGRect) {
        var newFrame: CGRect = frame
        if !self.frame.isEmpty {
            guard !newFrame.isEmpty else {
                return
            }
            newFrame.size.height = newFrame.size.height > 48.0 ? newFrame.size.height : 48.0
        }
        self.ck_setFrame(frame: newFrame)
    }
    
    open class func swizzledTabBarButtonFrame() {
        UIView.swizzlingTabBarButtonFrame
    }

}

Swift因?yàn)闆](méi)有+load方法,所以需要手動(dòng)去觸發(fā),我這里直接放到UIApplicationDelegate的代理方法中。

// MARK: - <UIApplicationDelegate>
    
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        // ...

        // 處理iOS 12.1系統(tǒng)tabBar圖標(biāo)偏移錯(cuò)位問(wèn)題
        UIView.swizzledTabBarButtonFrame()

        return true
    }

如果有任何疑問(wèn),歡迎留言或私信交流。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。