自定義tabbar被覆蓋問題

自定義tabbar的思路創(chuàng)建一個view取代tabbar。
但是很快就發(fā)現(xiàn)一個問題,當(dāng)我們dismiss或則popToViewController的時候,還是會出現(xiàn)系統(tǒng)的tabbarbutton。
很明顯是系統(tǒng)又添加了一遍tabbarbutton,添加子類必然會觸發(fā)layoutSubviews方法,因為layoutSubviews是布局子視圖的方法。如果能重寫layoutSubviews方法就能解決這個問題。
用黑魔法runtime就能替換掉UItabbar中的layoutSubviews方法,在布局子視圖的時候?qū)⑾到y(tǒng)自帶的UItabbarbutton給移除掉,
這段代碼是寫在UItabbar的分類中的:

#import "UITabBar+Category.h"
#import "MainTabBar.h"
@implementation UITabBar (Category)

+ (void)load{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        SEL systemSel = @selector(layoutSubviews);
        SEL lxqSel = @selector(remove_layoutSubviews);
        
        Method systemMethod = class_getInstanceMethod([self class], systemSel);
        Method lxqMethod = class_getInstanceMethod([self class], lxqSel);
        BOOL isAdd = class_addMethod(self, systemSel, method_getImplementation(lxqMethod), method_getTypeEncoding(lxqMethod));
        if (isAdd) {
            class_replaceMethod(self, lxqSel, method_getImplementation(systemMethod), method_getTypeEncoding(systemMethod));
        } else {
            method_exchangeImplementations(systemMethod, lxqMethod);
        }
    });
}

- (void)remove_layoutSubviews{
    NSArray *subviews = self.subviews;
    for (UIView *subview in subviews) {
        if ([subview isKindOfClass:[MainTabBar class]]) {
            
        } else {
            [subview removeFromSuperview];
        }
    }
    
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

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