自定義一個(gè)自己的tabbarController

要自定義一個(gè)tabbarController那肯定要先繼承UITabBarController了,然后在tabbarController中設(shè)置子控制器。

設(shè)置子控制器需要以下幾個(gè)步驟:

1. 設(shè)置子控制器的文字

childVc.title = title;// 同時(shí)設(shè)置tabbar和navigationBar的文字

2. 設(shè)置子控制器的圖片

childVc.tabBarItem.image = [UIImage imageNamed:image];

childVc.tabBarItem.selectedImage = [[UIImage imageNamed:selectedImage]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];//UIImageRenderingModeAlwaysOriginal 這個(gè)是總是用原圖片,如果不設(shè)置這個(gè)的話tabbar默認(rèn)都是藍(lán)色的

3. 設(shè)置文字的樣式

NSMutableDictionary *textAttrs = [NSMutableDictionary dictionary];

textAttrs[NSForegroundColorAttributeName] = [UIColor grayColor];

NSMutableDictionary *selectTextAttrs = [NSMutableDictionary dictionary];

selectTextAttrs[NSForegroundColorAttributeName] = [UIColor redColor];

[childVc.tabBarItem setTitleTextAttributes:textAttrs forState:UIControlStateNormal];

[childVc.tabBarItem setTitleTextAttributes:selectTextAttrs forState:UIControlStateSelected];

3.1 先給外面?zhèn)鬟M(jìn)來的小控制器 包裝 一個(gè)導(dǎo)航控制器 (也可以不設(shè)置,看自己心情)

FFNavigationController *nav = [[FFNavigationController alloc] initWithRootViewController:childVc];

4. 添加為子控制器

[self addChildViewController:nav];

以上都做完了可以通過kvc自定義一個(gè)自己的tabbar,要設(shè)置自己的tabbar只能通過kvc來設(shè)置,因?yàn)閁ITabBarController的tabbar是readonly的,不能直接設(shè)置

//UITabBarController的屬性

@property(nonatomic,readonly) UITabBar *tabBar NS_AVAILABLE_IOS(3_0); // Provided for -[UIActionSheet showFromTabBar:]. Attempting to modify the contents of the tab bar directly will throw an exception.

FFTabBar *tabBar = [[FFTabBar alloc] init];

tabBar.addbtnDelegate = self;

/** KVC */

[self setValue:tabBar forKey:@"tabBar"];

tabbarController基本就這樣了,現(xiàn)在看看tabBar里是怎樣的

?在- (void)layoutSubviews給tabbar重新布局

先添加中間大按鈕 (這個(gè)按鈕可以換成自己喜歡的樣子,這邊只是隨便搞了下,大概有個(gè)樣子而已)

UIButton *addBtn = [UIButton buttonWithType:UIButtonTypeCustom];

addBtn.backgroundColor = [UIColor redColor];

[addBtn addTarget:self action:@selector(addClickAction) forControlEvents:UIControlEventTouchUpInside];

[self addSubview:addBtn];

self.addBtn = addBtn;

CGFloat addBtnViewWidth = self.frame.size.width / count;

CGFloat addBtnViewHeight = self.frame.size.height + 50;

CGFloat addBtnViewX = (self.frame.size.width - addBtnViewWidth) * 0.5;

CGFloat addBtnViewY = -50;

addBtn.frame = CGRectMake(addBtnViewX, addBtnViewY, addBtnViewWidth, addBtnViewHeight);

確定了中間的大按鈕之后,然后旁邊的UITabBarButton的位置需要重新設(shè)置一下

NSUInteger idx = 0;

for (UIView *childView in self.subviews) {

Class class = NSClassFromString(@"UITabBarButton");

if ([childView isKindOfClass:class]){

if (idx == count / 2) idx++;

CGFloat cvX = idx * self.frame.size.width / count;//count是表示tabbar上一共有幾個(gè)按鈕

childView.frame = CGRectMake(cvX, 0, self.frame.size.width / count, self.frame.size.height);

idx++;

}

}

因?yàn)榇骲utton搞了超過tabbar的高度了所以要重寫一下hitTest方法,這個(gè)方法可以超出父控件的范圍仍然響應(yīng)點(diǎn)擊事件

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event{

UIView * view = [super hitTest:point withEvent:event];

if (view == nil) {

// 轉(zhuǎn)換坐標(biāo)系

CGPoint newPoint = [self.addBtn convertPoint:point fromView:self];

// 判斷觸摸點(diǎn)是否在button上

if (CGRectContainsPoint(self.addBtn.bounds, newPoint)) {

view = self.addBtn;

}

}

return view;

}

這樣tabbar上的item就基本設(shè)置完成了,再做個(gè)代理把中間按鈕的點(diǎn)擊事件放開出去就基本完成了

代碼鏈接在這

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

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

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