【发布时间】:2011-11-28 02:45:12
【问题描述】:
我试图隐藏特定视图的标签栏并在触摸屏幕时将其显示回来。我希望它具有与 youtube 相似的效果,例如,当播放视频时,播放器控件被隐藏并且触摸屏幕时再次显示控件。
【问题讨论】:
-
在这里查看我对类似问题的回答:stackoverflow.com/a/9141766/91458Best,
我试图隐藏特定视图的标签栏并在触摸屏幕时将其显示回来。我希望它具有与 youtube 相似的效果,例如,当播放视频时,播放器控件被隐藏并且触摸屏幕时再次显示控件。
【问题讨论】:
您可以使用此代码来显示和隐藏标签栏:
@implementation UITabBarController (Extras)
-(void)showTabBar:(BOOL)show {
UITabBar* tabBar = self.tabBar;
if (show != tabBar.hidden)
return;
UIView* subview = [self.view.subviews objectAtIndex:0];
CGRect frame = subview.frame;
frame.size.height += tabBar.frame.size.height * (show ? -1 : 1);
subview.frame = frame;
tabBar.hidden = !show;
}
此代码有效,最近被 Apple 在一个应用程序中接受,并且(作为一个类别)我发现比其他解决方案更易于使用。
当你想隐藏 tabBar 时,只需调用:
[self.tabBarController showTabBar:NO];
同样,要再次显示,请使用YES 作为参数调用此消息。
注意:不知何故,我忘记了我在过去的某个时候已经查看过此代码,现在我不确定最初是谁回答了它。索拉布answered a similar question。
Saurabh 提供的代码会遍历所有视图以查找 isKindOfClass:[UITabBar class],而我只获取第一个子视图——它在更新时可能很脆弱。
【讨论】:
您是否尝试过直接隐藏标签栏?
在视图控制器中,您希望没有标签栏,将tabBar.hidden = YES; 添加到viewWillAppear: 或viewDidAppear: 以反转具有内部触摸事件触发器tabBar.hidden = NO;
我没有亲自用标签栏做过这个,但这适用于其他视图,所以这是我首先尝试的方式。
【讨论】:
Try This Code
- (void) hideTabBar:(UITabBarController *) tabbarcontroller
{
CGRect screenRect = [[UIScreen mainScreen] bounds];
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.5];
float fHeight = screenRect.size.height;
if( UIDeviceOrientationIsLandscape([UIApplication sharedApplication].statusBarOrientation) )
{
fHeight = screenRect.size.width;
}
for(UIView *view in tabbarcontroller.view.subviews)
{
if([view isKindOfClass:[UITabBar class]])
{
[view setFrame:CGRectMake(view.frame.origin.x, fHeight, view.frame.size.width, view.frame.size.height)];
}
else
{
[view setFrame:CGRectMake(view.frame.origin.x, view.frame.origin.y, view.frame.size.width, fHeight)];
view.backgroundColor = [UIColor blackColor];
}
}
[UIView commitAnimations];
}
- (void) showTabBar:(UITabBarController *) tabbarcontroller
{
CGRect screenRect = [[UIScreen mainScreen] bounds];
float fHeight = screenRect.size.height - 49.0;
if( UIDeviceOrientationIsLandscape([UIApplication sharedApplication].statusBarOrientation) )
{
fHeight = screenRect.size.width - 49.0;
}
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.5];
for(UIView *view in tabbarcontroller.view.subviews)
{
if([view isKindOfClass:[UITabBar class]])
{
[view setFrame:CGRectMake(view.frame.origin.x, fHeight, view.frame.size.width, view.frame.size.height)];
}
else
{
[view setFrame:CGRectMake(view.frame.origin.x, view.frame.origin.y, view.frame.size.width, fHeight)];
}
}
[UIView commitAnimations];
}
【讨论】:
试试这个:
self.tabBarController.tabbar.hidden = YES;
把它放在 viewDidLoad 中。
【讨论】: