How to hide custom tab bar button when hidesBottom

2020-02-26 08:39发布

问题:

I am using the code snippet from Tito to add a custom button to my tab bar: https://github.com/tciuro/CustomTabBar

(Subclassing UITabbarController and adding a custom button using

// .. created a UIButton *button
[self.view addSubview:button];

)

This works great with my storyboard-based app except for the case of a subview within a navigation controller with the option "Hides bottom bar on push" enabled. This hides the tab bar as promised, but not the custom button. Seems like the button should be added as a subview to the tab bar itself? I tried this ugly code which did not even make the button show up:

for(UIView *view in self.view.subviews)
{
    if([view isKindOfClass:[UITabBar class]])
    {
        [view addSubview:button];
        break;
    }
}

Any ideas?

UPDATE: My solution: In my ApplicationDelegate i define the following methods, which i call whenever needed in the viewWillAppear or viewWillDisappear methods:

-(void)hideCenterButton:(BOOL)animated
{
    if(animated){

    [UIView animateWithDuration:0.3
                          delay:0.0f
                        options:UIViewAnimationCurveLinear
                     animations:^{
                         CGRect frame = self.centerButton.frame;
                         frame.origin.x = -100;
                         self.centerButton.frame = frame;
                     }
                     completion:^(BOOL finished){
                     }];
    }
}

-(void)showCenterButton:(BOOL)animated
{
    if(animated){

    [UIView animateWithDuration:0.35
                          delay:0.0f
                        options:UIViewAnimationCurveLinear
                     animations:^{
                         CGRect frame = self.centerButton.frame;
                         frame.origin.x = (self.view.superview.frame.size.width / 2) - (self.centerButton.frame.size.width / 2);
                         self.centerButton.frame = frame;
                     }
                     completion:^(BOOL finished){
                     }];
    }
}

I had to set the animation's duration to 0.35s to get a smooth effect in harmony with the tab bar.

回答1:

I think there are 2 ways you can got with this.

1) try to get the button into a view that is above the old top view controller and the tab bar BUT below the new top view controller that is pushed.

2) animate away the button when the new view controller is pushed.

The first will require mucking with the iOS proprietary view hierarchy which is undocumented, unsupported and could change anytime.

The second will be a matter of making the animation appear smooth enough for your user not to notice. It's not entirely a matter of behaving perfect, just appearing appropriately.

I would personally recommend an animation of the the button disappearing (animate it's alpha to 0) and reappearing based on if your view controller that goes over the tab bar is appearing or disappearing.

The animation for a navigation is (I believe) 0.3 seconds. If the button is in the middle of the tab bar, you'll likely want it invisible as the animating in view controller reaches it (if not sooner) so something between 0.1 and 0.15 seconds could be used to animate it out.

Now this does not make the button behave exactly the same as the tab bar, but with the quickness of the transition being so short, it will be unnoticeable really to the user.

Now just to provide a question for you to ask yourself. Why do you need to push a view controller that overlaps the tab bar? Why is that more desirable/necessary than presenting a modal view controller? If you can strongly argue for it, keep at it and good luck, if it's not necessary however, you may be able to achieve the experience you want with a modal view controller.



回答2:

One easy way to handle this would be to create an instance of the button in .h of your file.

UIButton *customTabButton;

When calling the hides bottom bar on push set the button property to hidden and reset it again in the other views if the bottom bar is visible.

    shareFbButton.hidden=YES;

You can check this is the viewDidLoad of all the files and put this line of code if needed to make sure you are displaying the button and hiding the button on all the pages you need.

 if(self.tabBarController.tabBar.isHidden){

 // set or reset the custom button visibility here 
}

This is one way.



回答3:

Why don't you make button your tabbar's part.

tabBarController.tabBar.addSubView(yourButton)

everything would be solve. cheers!



回答4:

Check this one to put a button on the UITabBar. See if it works after with hidesBottoBarWhenPushed.