我正在使用 Tito 的代码片段将自定义按钮添加到我的标签栏:https://github.com/tciuro/CustomTabBar
I am using the code snippet from Tito to add a custom button to my tab bar: https://github.com/tciuro/CustomTabBar
(继承 UITabbarController 并使用添加自定义按钮
(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;
}
}
有什么想法吗?
更新:我的解决方案:在我的 ApplicationDelegate 中,我定义了以下方法,只要需要,我就会在 viewWillAppear 或 viewWillDisappear 方法中调用它们:
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){
}];
}
}
我必须将动画的持续时间设置为 0.35 秒才能获得与标签栏相协调的平滑效果.
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) 尝试将按钮放入旧顶视图控制器上方的视图和标签栏但在被推送的新顶视图控制器下方的视图中.
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) 当新的视图控制器被按下时,按钮动画消失.
2) animate away the button when the new view controller is pushed.
第一个需要处理未记录、不受支持且随时可能更改的 iOS 专有视图层次结构.
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.
我个人会推荐按钮消失的动画(将其 alpha 设置为 0)并根据您的视图控制器越过标签栏是出现还是消失而重新出现.
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.
导航动画是(我相信)0.3 秒.如果按钮位于标签栏的中间,您可能希望它在视图控制器中的动画到达它时不可见(如果不是更早的话),因此可以使用 0.1 到 0.15 秒之间的时间将其动画出来.
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"时如何隐藏自定义标签栏按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!