自定義一個自己的tabbarController

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

設置子控制器需要以下幾個步驟:

1. 設置子控制器的文字

childVc.title = title;// 同時設置tabbar和navigationBar的文字

2. 設置子控制器的圖片

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

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

3. 設置文字的樣式

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 先給外面傳進來的小控制器 包裝 一個導航控制器 (也可以不設置,看自己心情)

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

4. 添加為子控制器

[self addChildViewController:nav];

以上都做完了可以通過kvc自定義一個自己的tabbar,要設置自己的tabbar只能通過kvc來設置,因為UITabBarController的tabbar是readonly的,不能直接設置

//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基本就這樣了,現在看看tabBar里是怎樣的

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

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

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的位置需要重新設置一下

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上一共有幾個按鈕

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

idx++;

}

}

因為大button搞了超過tabbar的高度了所以要重寫一下hitTest方法,這個方法可以超出父控件的范圍仍然響應點擊事件

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

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

if (view == nil) {

// 轉換坐標系

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

// 判斷觸摸點是否在button上

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

view = self.addBtn;

}

}

return view;

}

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

代碼鏈接在這

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容