当hidesBottomBarWhenPushed为“TRUE”时,如何隐藏自定义标签栏按钮 [英] How to hide custom tab bar button when hidesBottomBarWhenPushed is "TRUE"

查看:135
本文介绍了当hidesBottomBarWhenPushed为“TRUE”时,如何隐藏自定义标签栏按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Tito的代码片段向我的标签栏添加自定义按钮:
https: //github.com/tciuro/CustomTabBar



(对UITabbarController进行子类化并使用



<添加自定义按钮pre> // ..创建了一个UIButton *按钮
[self.view addSubview:button];



这适用于我的基于故事板的应用程序,除了导航控制器中的子视图与选项启用时隐藏底栏。
这会按照承诺隐藏标签栏,但不会隐藏自定义按钮。
好​​像按钮应该作为子视图添加到标签栏本身?
我尝试了这个丑陋的代码甚至没有显示按钮:

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

任何想法?



更新:
我的解决方案:
在我的ApplicationDelegate中,我定义了以下方法,我在viewWillAppear或viewWillDisappear方法中需要时调用:

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

[UIView animateWithDuration:0.3
延迟:0.0f
选项:UIViewAnimationCurveLinear
动画:^ {
CGRect frame = self.centerButton.frame;
frame.origin.x = -100;
self.centerButton.frame = frame;
}
完成:^(BOOL完成){
}];
}
}

- (无效)showCenterButton:(BOOL)动画
{
if(animated){

[UIView animateWithDuration:0.35
延迟:0.0f
选项:UIViewAnimationCurveLinear
动画:^ {
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;
}
完成:^(BOOL完成){
}];
}
}

我必须将动画的持续时间设置为0.35秒获得与标签栏协调的平滑效果。

解决方案

我认为有两种方法可以解决这个问题。



1)尝试将按钮放入旧顶视图控制器上方的视图和按下的新顶视图控制器下方的标签栏BUT。



2)在按下新视图控制器时动画显示按钮。



第一个需要与iOS专有视图混淆层次结构,没有文档记录,不受支持,可以随时更改。



第二个问题是让动画显得足够流畅,让用户不要注意。这并不完全是表现完美的问题,只是恰当地出现。



我个人会推荐按钮消失的动画(将其动画为α的动画为0)并在此基础上重新出现如果超过标签栏的视图控制器出现或消失。



导航的动画是(我相信)0.3秒。如果按钮位于标签栏的中间,您可能希望它不可见,因为视图控制器中的动画到达它(如果不是更快),因此可以使用0.1到0.15秒之间的动画来为其设置动画。



现在这不会使按钮的行为与标签栏完全相同,但由于过渡的速度太短,对用户来说真的是不明显的。



现在只是提出一个问题让你问自己。为什么需要推动与标签栏重叠的视图控制器?为什么比呈现模态视图控制器更可取/更必要?如果您可以强烈争辩,请坚持下去并祝您好运,但如果没有必要,您可以通过模态视图控制器获得您想要的体验。


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.

解决方案

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.

这篇关于当hidesBottomBarWhenPushed为“TRUE”时,如何隐藏自定义标签栏按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆