按下视图设置HidesBottomBarWhenPushed时隐藏中心选项卡栏按钮

时间:2012-01-07 09:22:57

标签: objective-c ios uitabbar

我正在关注如何使用像Path,Instagram等中心按钮创建标签栏的示例:http://idevrecipes.com/2010/12/16/raised-center-tab-bar-button/

我遇到的问题是当一个视图被推到堆栈上时,HidesBottomBarWhenPush设置隐藏标签栏,中心按钮仍会显示。

在评论中,其他几个人遇到了这个问题,但没有可行的解决方案。 (我在评论中尝试了所有建议的解决方案)

我提出了一个hacky解决方案 - 在一个不相关的单例类中存储对中心按钮的引用,然后让推出的视图在加载时隐藏按钮,并在它消失时取消隐藏 - 但这只是感觉不对,看起来很有趣,因为你可以在推动视图动画开始之前看到按钮消失。

有人有这个工作吗?

3 个答案:

答案 0 :(得分:6)

我遇到了同样的问题。 我通过覆盖以下viewDidLayoutSubviews方法(按钮是我的中心按钮)编辑了BaseViewController.m(我的UITabBarController子类),如下所示。

- (void)viewDidLayoutSubviews{
    button.center = self.tabBar.center;
}

现在您的按钮跟随标签栏。

答案 1 :(得分:2)

您必须使用UIImageView并将其添加到tabBar:

- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:               (UIViewController *)viewController
{
    if (tabBarController.selectedIndex != AUCenterTabBarButtonIntex) {
        self.centerImageView.highlighted = NO;
    } else {
        self.centerImageView.highlighted = YES;
        self.selectedIndex = AUCenterTabBarButtonIntex;
    }

}


- (void)addCenterImageViewWithImage:(UIImage *)image highlitedImage:(UIImage *)highlitedImage
{
    UIImageView *centerImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, image.size.width/2, image.size.height/2)];
    centerImageView.autoresizingMask = UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleTopMargin;

    centerImageView.image = image;
    centerImageView.highlightedImage = highlitedImage;

    CGFloat heightDifference = centerImageView.frame.size.height - self.tabBar.frame.size.height;
    if (heightDifference < 0)
        centerImageView.center = CGPointMake(self.tabBar.center.x, centerImageView.center.y);
    else
    {
        CGPoint center = self.tabBar.center;
        center.y = (self.tabBar.frame.size.height/2) - (heightDifference/2);
        centerImageView.center = center;
    }

    [self.tabBar addSubview:centerImageView];

    self.centerImageView = centerImageView;
}

答案 2 :(得分:1)

在推送UIViewController之前,将自定义按钮添加到UITabBar

弹出UIViewController后,将自定义按钮恢复为self.view

子类UITabViewController

NSArray *array=  self.viewControllers;
for(UIViewController *controller in array){
if([controller isKindOfClass:[UINavigationController class]]){
    UINavigationController *navigationController=(UINavigationController*)controller;
    navigationController.delegate=self;
   }
}

实施委托方法

- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
if (viewController.hidesBottomBarWhenPushed) {
    CGRect rect= [button convertRect:button.bounds toView:self.tabBar];
    [button removeFromSuperview];
    [self.tabBar addSubview:button];
    button.frame=rect;
 }
}
-(void)navigationController:(nonnull UINavigationController *)navigationController didShowViewController:(nonnull UIViewController *)viewController animated:(BOOL)animated{
if(!viewController.hidesBottomBarWhenPushed){
    CGRect rect= [button convertRect:button.bounds toView:self.view];
    [button removeFromSuperview];
    [self.view addSubview:button];
    button.frame=rect;
}
}