有時候我們需要在UITabBarController中間添加一個按鈕桑阶,并且不需要讓他控制VC。我們只需要簡單幾步就可以實現(xiàn)此效果勾邦。
一:繼承UITabBarController
@interface NNTabBarController ()
@property (nonatomic, weak) UIButton *composeButton;
@end
@implementation NNTabBarController
#pragma mark - 統(tǒng)一設(shè)置所有 UITabBarItem 的文字屬性
+ (void)initialize {
NSMutableDictionary *attrs = [NSMutableDictionary dictionary];
attrs[NSFontAttributeName] = [UIFont systemFontOfSize:12];
attrs[NSForegroundColorAttributeName] = [UIColor grayColor];
NSMutableDictionary *selectedAttrs = [NSMutableDictionary dictionary];
selectedAttrs[NSFontAttributeName] = attrs[NSFontAttributeName];
selectedAttrs[NSForegroundColorAttributeName] = [UIColor colorWithRed:255/255.0 green:125/255.0 blue:0 alpha:1];
UITabBarItem *items = [UITabBarItem appearance];
[items setTitleTextAttributes:attrs forState:UIControlStateNormal];
[items setTitleTextAttributes:selectedAttrs forState:UIControlStateSelected];
}
二:加載vc和中間按鈕
#pragma mark - 初始化
- (instancetype)init {
if (self = [super init]) {
[self addChildViewControllers];
[self addComposeButton];
}
return self;
}
#pragma mark - 添加所有子控制器
- (void)addChildViewControllers {
[self addChildViewControllerClassName:@"NNHomePageController" title:@"首頁" imageName:@"tabbar_home"];
[self addChildViewControllerClassName:@"NNFoundController" title:@"發(fā)現(xiàn)" imageName:@"tabbar_discover"];
[self addChildViewController: [[UIViewController alloc] init]];
[self addChildViewControllerClassName:@"NNMessageController" title:@"消息" imageName:@"tabbar_message_center"];
[self addChildViewControllerClassName:@"NNMyController" title:@"我的" imageName:@"tabbar_profile"];
}
#pragma mark - 設(shè)置所有子控制器
- (void)addChildViewControllerClassName:(NSString *)className title:(NSString *)title imageName:(NSString *)imageName
{
UIViewController *viewController = [[NSClassFromString(className) alloc] init];
NNNavigationController *nav = [[NNNavigationController alloc] initWithRootViewController:viewController];
viewController.title = title;
viewController.view.backgroundColor = [UIColor whiteColor];
viewController.tabBarItem.image = [UIImage imageNamed: imageName];
viewController.tabBarItem.selectedImage = [UIImage imageNamed:[NSString stringWithFormat:@"%@_highlighted", imageName]];
[self addChildViewController:nav];
}
#pragma mark - 添加中間的按鈕
- (void)addComposeButton {
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setImage:[UIImage imageNamed:@"tabBar_publish_icon"] forState:UIControlStateNormal];
[button setImage:[UIImage imageNamed:@"tabBar_publish_icon_highlighted"] forState:UIControlStateSelected];
[button addTarget:self action:@selector(composeButtonClick:) forControlEvents:UIControlEventTouchUpInside];
self.composeButton = button;
[self.tabBar addSubview:button];
[self.tabBar bringSubviewToFront:button];
CGFloat width = self.tabBar.bounds.size.width / self.childViewControllers.count - 2;
self.tabBar.tintColor = [UIColor colorWithRed:68/255.0 green:173/255.0 blue:159/255.0 alpha:1];
button.frame = CGRectInset(self.tabBar.bounds, 2 * width, 0);
}
#pragma mark - 點擊寫文章按鈕
- (void)composeButtonClick:(UIButton *)button {
NSLog(@"\n點擊了寫文章按鈕");
}
三:在視圖剛要出現(xiàn)的時候講將中間按鈕置前
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
// 不置前點擊中間按鈕會導(dǎo)致黑屏
[self.tabBar bringSubviewToFront:self.composeButton];
}
end