LCWechat --完美封裝 tabBar

1.自定義TabBarController

1.自定義繼承于UITabBarController
2.利用KVC 替換系統(tǒng)的tabBar
3. 利用appearance 全局統(tǒng)一設(shè)置UITabBarItem
[self setValue:[[LCTabBar alloc] init] forKeyPath:@"tabBar"];
 NSMutableDictionary *normalAttrs = [NSMutableDictionary dictionary];
  normalAttrs[NSForegroundColorAttributeName] = [UIColor grayColor]; normalAttrs[NSFontAttributeName] = [UIFont systemFontOfSize:12];

  NSMutableDictionary *selectedAttrs = [NSMutableDictionary dictionary];
   selectedAttrs[NSForegroundColorAttributeName] = [UIColor darkGrayColor];
    
 UITabBarItem *item = [UITabBarItem appearance];
  [item setTitleTextAttributes:normalAttrs forState:UIControlStateNormal];
  [item setTitleTextAttributes:selectedAttrs forState:UIControlStateSelected];

2.自定義tabBar

1.自定義繼承于UITabBar
2.初始化
3.布局

2.1中間有線條的tabBar

如圖1


1.png

解決方案: 布局子控件地方, 將中間按鈕 bringSubviewToFront

- (void)layoutSubviews
{
    [super layoutSubviews];
    
    CGFloat width = self.width;
    CGFloat height = self.height;
    
    self.plusButton.center = CGPointMake(width * 0.5, height * 0.5);
    self.plusButton.y = -20;
    
    int index = 0;
    
    CGFloat tabBarButtonW = width / 5;
    CGFloat tabBarButtonH = height;
    CGFloat tabBarButtonY = 0;

    for (UIView *tabBarButton in self.subviews) {
        if (![NSStringFromClass(tabBarButton.class) isEqualToString:@"UITabBarButton"]) continue;
        
        
        CGFloat tabBarButtonX = index * tabBarButtonW;
        if (index >= 2) {
            tabBarButtonX += tabBarButtonW;
        }
        
        
        tabBarButton.frame = CGRectMake(tabBarButtonX, tabBarButtonY, tabBarButtonW, tabBarButtonH);
        
        index++;
    }
    
    [self bringSubviewToFront:self.plusButton];
}

2.2中間按鈕 超出tabBar的范圍不能響應(yīng)點(diǎn)擊事件

如圖 2

2.png

解決方案: 重寫系統(tǒng)的hitTest方法, 不了解的看官可以去查查事件傳遞和響應(yīng)者鏈條


- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
    
    if (self.isHidden == NO) { // 當(dāng)前界面 tabBar顯示
        
        CGPoint newPoint = [self convertPoint:point toView:self.plusButton];
        
        if ( [self.plusButton pointInside:newPoint withEvent:event]) { // 點(diǎn) 屬于按鈕范圍
            return self.plusButton;
            
        }else{
            return [super hitTest:point withEvent:event];
        }
    }
    else {
        return [super hitTest:point withEvent:event];
    }
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

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

  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 178,716評論 25 709
  • 在iOS開發(fā)中經(jīng)常會涉及到觸摸事件。本想自己總結(jié)一下,但是遇到了這篇文章,感覺總結(jié)的已經(jīng)很到位,特此轉(zhuǎn)載。作者:L...
    WQ_UESTC閱讀 6,236評論 4 26
  • 好奇觸摸事件是如何從屏幕轉(zhuǎn)移到APP內(nèi)的?困惑于Cell怎么突然不能點(diǎn)擊了?糾結(jié)于如何實(shí)現(xiàn)這個奇葩響應(yīng)需求?亦或是...
    Lotheve閱讀 59,425評論 51 604
  • 昨夜里,夜深仍未入眠,翻開手機(jī),發(fā)現(xiàn)昆明有雨,乍一驚,才記起3月5日,正值驚蟄。正當(dāng)時,腦子里跳入五個字——草木一...
    戎十六閱讀 613評論 0 2
  • 杭城,一座讓人想要慢點(diǎn)的城市。 大學(xué)畢業(yè)后,選擇在這里開始,是因?yàn)橐粋€重要的人在這個城市。 想來到你在的城市...
    狒狒shi小胖子閱讀 262評論 0 1

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