【发布时间】:2012-06-28 20:30:57
【问题描述】:
我正在使用 Tito 的代码 sn-p 将自定义按钮添加到我的标签栏: https://github.com/tciuro/CustomTabBar
(继承 UITabbarController 并使用添加自定义按钮
// .. created a UIButton *button
[self.view addSubview:button];
)
这对我的基于故事板的应用程序非常有用,但导航控制器中的子视图启用了“在推送时隐藏底栏”选项的情况除外。 这会按承诺隐藏标签栏,但不会隐藏自定义按钮。 似乎按钮应该作为子视图添加到标签栏本身? 我尝试了这个丑陋的代码,它甚至没有显示按钮:
for(UIView *view in self.view.subviews)
{
if([view isKindOfClass:[UITabBar class]])
{
[view addSubview:button];
break;
}
}
有什么想法吗?
更新: 我的解决方案: 在我的 ApplicationDelegate 中,我定义了以下方法,只要需要,我就会在 viewWillAppear 或 viewWillDisappear 方法中调用它们:
-(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 秒才能获得与标签栏相协调的平滑效果。
【问题讨论】:
-
嘿,
UIViewAnimationCurveLinear会抛出警告,你应该改用UIViewAnimationOptionCurveLinear。 -
经过一些测试,看起来最自然的动画选项是
UIViewAnimationOptionCurveEaseInOut -
我测试了你的代码,它可以工作,但是按钮返回到标签栏后面,有什么解决办法吗?
标签: ios ios5 uitabbarcontroller