實現如下:
// Create a custom UIButton and add it to the center of our tab bar
-(void) addCenterButtonWithImage:(UIImage*)buttonImage highlightImage:(UIImage*)highlightImage
{
UIButton* button = [UIButton buttonWithType:UIButtonTypeCustom];
button.autoresizingMask = UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleTopMargin;
button.frame = CGRectMake(0.0, 0.0, buttonImage.size.width, buttonImage.size.height);
[button setBackgroundImage:buttonImage forState:UIControlStateNormal];
[button setBackgroundImage:highlightImage forState:UIControlStateHighlighted];
CGFloat heightDifference = buttonImage.size.height - self.tabBar.frame.size.height;
if (heightDifference < 0)
button.center = self.tabBar.center;
else
{
CGPoint center = self.tabBar.center;
center.y = center.y - heightDifference/2.0;
button.center = center;
}
[self.view addSubview:button];
}
如果我們要導航到下一層並且要隱藏底部的UITabBar,代碼如下:
UIViewController *vc = [[UIViewController alloc] init];
vc.hidesBottomBarWhenPushed = YES;
[self.navigationController pushViewController:vc animated:YES];
通過將hidesBottomBarWhenPushed屬性設置爲YES,壓棧的時候可以把UITabBar隱藏,但是中間的按鈕並沒有被隱藏。這裏有兩種解決方法:
一、提供方法手工隱藏/顯示中間按鈕,顯示的時候觸發動畫
- (void)hideTabBar:(BOOL)hidden {
UIButton *btn = (UIButton *)[self.tabBarController.view viewWithTag:kCENTER_BUTTON_TAG];
[[btn layer] removeAnimationForKey:kANIMATION_KEY];
if (btn.hidden) {
CATransition *animation = [CATransition animation];
animation.delegate = self;
animation.duration = 0.35;// 0.618
animation.timingFunction = UIViewAnimationCurveEaseInOut;
animation.type = kCATransitionFade;
[[btn layer] addAnimation:animation forKey:kANIMATION_KEY];
}
btn.hidden = hidden;
}
二、自動隱藏
1、實現UINavigationControllerDelegate協議,爲UITabBarController的viewControllers註冊協議
- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
if (viewController.hidesBottomBarWhenPushed) {
[self hideTabBar:viewController.hidesBottomBarWhenPushed];
}
}
[self.tabBarController.viewControllers enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
UINavigationController *navVC = obj;
navVC.delegate = self;
}];
2、註冊UITabBar的hidden的kvo
[self.tabBarController addObserver:self forKeyPath:@"tabBar.hidden" options:NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld context:nil];
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
if ([keyPath isEqualToString:kTABBAR_HIDDEN_KEYPATH]) {
[self hideTabBar:self.tabBar.hidden];
}
}
思路:
1、因爲設置hidesBottomBarWhenPushed爲YES,導致UITabBar的hidden屬性變化,所以一開始希望通過註冊UITabBar的hidden的kvo來實現自動隱藏/顯示中間按鈕,但是在UINavigationController壓棧完成後才觸發kvo事件,導致進入下一層裏面才調用隱藏中間按鈕的方法;
2、接着就實現UINavigationControllerDelegate協議,爲UITabBarController的viewControllers註冊協議,在UINavigationController壓棧的時候觸發方法navigationController:willShowViewController:animated,判斷hidesBottomBarWhenPushed屬性設置如果設置爲YES,則直接隱藏中間按鈕;
3、結果是只要在UINavigationController壓棧的時候設置viewController的hidesBottomBarWhenPushed屬性爲YES,壓棧的時候就可以直接隱藏中間按鈕,出棧的時候觸發中間按鈕的顯示動畫。
在UITabBar中間添加按鈕的實現:https://github.com/boctor/idev-recipes
例子代碼下載地址:http://vdisk.weibo.com/s/s_B6F