Iphone:是否可以隐藏 TabBar?(iOS 8 之前)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1982172/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Iphone: Is it possible to hide the TabBar? (Pre-iOS 8)
提问by Steve
I have an application that uses a UITabBarController
to switch between modes. When in a certain mode, I'd like to hide the tab bar until the steps of that mode have been completed. Note that I'm not using a navigation controller so I can't use the setHidesBottomBarWhenPushed
method on the navigation controller to hide the tab bar.
我有一个使用 aUITabBarController
在模式之间切换的应用程序。在某种模式下,我想隐藏标签栏,直到该模式的步骤完成。请注意,我没有使用导航控制器,因此我无法使用setHidesBottomBarWhenPushed
导航控制器上的方法来隐藏选项卡栏。
Prior to iOS 8, When I attempt to hide the tarbar using:
在 iOS 8 之前,当我尝试使用以下方法隐藏 tarbar 时:
self.tabBarController.tabBar.hidden = YES
the tab bar goes away, but it leaves a 50 pixel blank area at the bottom of the screen where the tab bar used to be. I can't seem to figure out how to fill that area. Anything in the UI that is in that area is clipped and cannot be seen.
标签栏消失了,但它在屏幕底部留下了一个 50 像素的空白区域,该区域曾经是标签栏。我似乎无法弄清楚如何填充该区域。位于该区域的 UI 中的任何内容都被剪裁并且无法看到。
Any ideas if this is even possible? I'd really like to stay away from the navigation controller.
如果这可能的话,有什么想法吗?我真的很想远离导航控制器。
回答by bshirley
Here's my code for that:
这是我的代码:
This is, of course, mucking with the goings onin the controller's view hierarchy. It could change/break. This uses defined APIs, so Apple won't care, but they won't care about breaking your code, either.
当然,这与控制器视图层次结构中发生的事情有关。它可能会改变/中断。这使用定义的 API,因此 Apple 不会关心,但他们也不会关心破坏您的代码。
- (void)hideTabBar {
UITabBar *tabBar = self.tabBarController.tabBar;
UIView *parent = tabBar.superview; // UILayoutContainerView
UIView *content = [parent.subviews objectAtIndex:0]; // UITransitionView
UIView *window = parent.superview;
[UIView animateWithDuration:0.5
animations:^{
CGRect tabFrame = tabBar.frame;
tabFrame.origin.y = CGRectGetMaxY(window.bounds);
tabBar.frame = tabFrame;
content.frame = window.bounds;
}];
// 1
}
- (void)showTabBar {
UITabBar *tabBar = self.tabBarController.tabBar;
UIView *parent = tabBar.superview; // UILayoutContainerView
UIView *content = [parent.subviews objectAtIndex:0]; // UITransitionView
UIView *window = parent.superview;
[UIView animateWithDuration:0.5
animations:^{
CGRect tabFrame = tabBar.frame;
tabFrame.origin.y = CGRectGetMaxY(window.bounds) - CGRectGetHeight(tabBar.frame);
tabBar.frame = tabFrame;
CGRect contentFrame = content.frame;
contentFrame.size.height -= tabFrame.size.height;
}];
// 2
}
Edit: An anonymous user has suggested the following addition for 7.0 (i have not tested this, and could not say whether it is a workaround or an ideal implementation):
编辑:匿名用户建议为 7.0 添加以下内容(我尚未对此进行测试,也无法说明它是解决方法还是理想的实现):
// 1. To Hide the black line in IOS7 only, this extra bit is required
if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"7.0")) {
[self.tabBarController.tabBar setTranslucent:YES];
}
// 2. For IOS 7 only
if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"7.0")) {
[self.tabBarController.tabBar setTranslucent:NO];
}
Edit: Entirely untested in 8.x and likely lacking in some layouts.
编辑:在 8.x 中完全未经测试,可能缺少某些布局。
回答by Terence
Like Steve, I haven't found a clean way to do this (even though Apple Photopicker does something similar). Here is what I have done:
像史蒂夫一样,我还没有找到一种干净的方法来做到这一点(尽管 Apple Photopicker 做了类似的事情)。这是我所做的:
if (systemAction)
{
// Reveal tab bar back
CGRect bounds = [[UIScreen mainScreen] bounds];
CGRect tabBarFrame = self.tabBarController.tabBar.frame;
self.tabBarController.view.frame = CGRectMake(0,0,bounds.size.width,bounds.size.height);
self.toolBar.hidden = YES;
systemAction = NO;
}
else
{
//hide tab bar
CGRect bounds = [[UIScreen mainScreen] bounds];
CGRect tabBarFrame = self.tabBarController.tabBar.frame;
CGRect navigationBarFrame = self.navigationController.navigationBar.frame;
self.tabBarController.view.frame = CGRectMake(0,0,bounds.size.width,bounds.size.height+tabBarFrame.size.height);
self.toolBar.hidden = NO;
CGRect frame = self.toolBar.frame;
frame.origin.y = bounds.size.height - frame.size.height - navigationBarFrame.size.height;
self.toolBar.frame = frame;
systemAction = YES;
}
What it is doing is pushing the view down so I can display a toolbar (and not hiding it). Obviously this is for only the 'root view' of a tabbar + navigation controller. For any subsequent views you can set the 'hidesBottomBarWhenPushed' on the viewcontroller you are pushing.
它正在做的是将视图向下推,以便我可以显示工具栏(而不是隐藏它)。显然,这仅适用于标签栏 + 导航控制器的“根视图”。对于任何后续视图,您可以在要推送的视图控制器上设置“hidesBottomBarWhenPushed”。
回答by GAllan
I tried a number of the solutions above, but no joy in iOS 8. I find that setting in viewWillAppear the following works for me. Should work in iOS 7 as the extendedLayoutIncludesOpaqueBars was introduced then.
我尝试了上面的许多解决方案,但在 iOS 8 中没有乐趣。我发现 viewWillAppear 中的设置对我有用。应该在 iOS 7 中工作,因为那时引入了 extendedLayoutIncludesOpaqueBars。
self.extendedLayoutIncludesOpaqueBars = true
self.tabBarController?.tabBar.isHidden = true
self.tabBarController?.tabBar.isOpaque = true
and if you need to turn tabBars on again when you leave to use the following in viewWillDisappear.
如果您在离开时需要再次打开 tabBars 以在 viewWillDisappear 中使用以下内容。
self.tabBarController?.tabBar.isHidden = false
self.tabBarController?.tabBar.isOpaque = false
I use this to allow a return from a transition to keep the TabBar
hidden. Not used it in a button action but if like me you find nothing above now works, this could be the basis of a programmable solution.
我使用它来允许从转换返回以保持TabBar
隐藏。没有在按钮操作中使用它,但如果像我一样你发现上面没有任何工作,这可能是可编程解决方案的基础。
回答by coder9
I use only this single line to achieve this. I use prepareForSegue method before showing the view controller having the tab bar.
我只使用这一行来实现这一点。在显示具有标签栏的视图控制器之前,我使用了 prepareForSegue 方法。
-(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
if([segue.identifier isEqualToString:@"showLogin"]){
[segue.destinationViewController setHidesBottomBarWhenPushed:YES];
}
}
回答by Ted
It's a bit late in the day, but of all the answers to the question that I've trawled through this afternoon, this is the one that worked best for me.
今天有点晚了,但是在我今天下午浏览的所有问题的答案中,这是最适合我的答案。
How to hide uitabbarcontroller
// Method call
[self hideTabBar:self.tabBarController];
// Method implementations
- (void)hideTabBar:(UITabBarController *) tabbarcontroller
{
[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, 480, view.frame.size.width, view.frame.size.height)];
}
else
{
[view setFrame:CGRectMake(view.frame.origin.x, view.frame.origin.y, view.frame.size.width, 480)];
}
}
[UIView commitAnimations];
}
- (void)showTabBar:(UITabBarController *) tabbarcontroller
{
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.5];
for(UIView *view in tabbarcontroller.view.subviews)
{
NSLog(@"%@", view);
if([view isKindOfClass:[UITabBar class]])
{
[view setFrame:CGRectMake(view.frame.origin.x, 431, view.frame.size.width, view.frame.size.height)];
}
else
{
[view setFrame:CGRectMake(view.frame.origin.x, view.frame.origin.y, view.frame.size.width, 431)];
}
}
[UIView commitAnimations];
}
回答by Abduliam Rehmanius
I had worked on almost the same case, actually used the code from http://www.developers-life.com/hide-uitabbarcontrolleruitabbar-with-animation.htmland made it better according to my needs, this might help others too.
我曾处理过几乎相同的案例,实际上使用了来自http://www.developers-life.com/hide-uitabbarcontrolleruitabbar-with-animation.html的代码,并根据我的需要进行了改进,这也可能对其他人有所帮助。
I am using a UISplitViewController as the root view controller and its detail portion is a UITabBarController, I had to hide the tabbar in portrait mode:
我使用 UISplitViewController 作为根视图控制器,它的细节部分是一个 UITabBarController,我不得不在纵向模式下隐藏标签栏:
// In UITabBarController's custom implementation add following method,
// this method is all that will do the trick, just call this method
// whenever tabbar needs to be hidden/shown
- (void) hidetabbar:(NSNumber*)isHidden {
UITabBarController *tabBarController=self;
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.5];
CGRect tabbarFrame=CGRectZero;
for(UIView *theView in tabBarController.view.subviews) {
//NSLog(@"%@", view);
if([theView isKindOfClass:[UITabBar class]]) {
tabbarFrame=theView.frame;
if ([isHidden boolValue]) {
tabbarFrame=CGRectMake(tabbarFrame.origin.x,
tabBarController.view.frame.size.height,
tabbarFrame.size.width,
tabbarFrame.size.height);
} else {
tabbarFrame=CGRectMake(tabbarFrame.origin.x,
tabBarController.view.frame.size.height - tabbarFrame.size.height,
tabbarFrame.size.width,
tabbarFrame.size.height);
}
theView.frame=tabbarFrame;
break;
}
}
for(UIView *theView in tabBarController.view.subviews) {
if(![theView isKindOfClass:[UITabBar class]]) {
CGRect theViewFrame=theView.frame;
if ([isHidden boolValue]) {
theViewFrame=CGRectMake(theViewFrame.origin.x,
theViewFrame.origin.y,
theViewFrame.size.width,
theViewFrame.size.height + tabbarFrame.size.height);
} else {
theViewFrame=CGRectMake(theViewFrame.origin.x,
theViewFrame.origin.y,
theViewFrame.size.width,
theViewFrame.size.height - tabbarFrame.size.height);
}
theView.frame=theViewFrame;
}
}
[UIView commitAnimations];
}
I used following code to call the hidetabbar: method
我使用以下代码来调用 hidetabbar: 方法
//In my UISplitViewController's custom implementation
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
@synchronized(self){
//change the self.splitDetailController to your UITabBarController's object
[self.splitDetailController
performSelector:@selector(hidetabbar:)
withObject:[NSNumber numberWithBool:UIInterfaceOrientationIsLandscape(interfaceOrientation)]
afterDelay:0.5];
}
return YES;
}
I tested this code to work in simulator only, let me know if it works on device too ;-)
我测试了这段代码只能在模拟器中工作,让我知道它是否也适用于设备;-)
回答by user2159978
autoresizing mask has an enumeration. Try to set all the options and check if autoresize subviews option is checked in parent view
autoresizing mask 有一个枚举。尝试设置所有选项并检查是否在父视图中选中了 autoresize subviews 选项
回答by MystikSpiral
Do you have the autoResizingMask set on the sub view?
您是否在子视图上设置了 autoResizingMask?
view.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
Something like that should do the trick and allow the view sitting atop the stack to re-size.
这样的事情应该可以解决问题,并允许位于堆栈顶部的视图重新调整大小。
回答by matt
The obvious solution, keeping your original architecture, would have been to present that view modally:
保留原始架构的显而易见的解决方案是以模态方式呈现该视图:
- (void)tabBarController:(UITabBarController *)tb
didSelectViewController:(UIViewController *)vc {
if (tb.selectedIndex == MODALONE) {
UIViewController* mod =
[[UIViewController alloc] initWithNibName: @"ModalView"
bundle: nil];
[tb presentModalViewController:mod animated:NO];
[mod release];
}
}
The view now covers the entire screen (except for the status bar is there is one) including the tab bar, so it looks as if the tab bar has gone away in response to the user pressing that tab bar item.
该视图现在覆盖了整个屏幕(除了状态栏有一个)包括标签栏,所以看起来标签栏已经消失以响应用户按下该标签栏项目。
回答by ChintaN -Maddy- Ramani
You can create Tabbar Category and show/Hide easily. and you can access full view.
您可以轻松创建 Tabbar 类别并显示/隐藏。您可以访问完整视图。
create category #import "UITabBarController+HideTabBar.h"
创建类别 #import "UITabBarController+HideTabBar.h"
@implementation UITabBarController (HideTabBar)
- (void)hideTabBarAnimated:(BOOL)animated
{
CGRect statusbarFrame = [UIApplication sharedApplication].statusBarFrame;
CGRect tabBarControllerFrame = self.view.frame;
if (statusbarFrame.size.height>20)
{
tabBarControllerFrame.size.height = screenSize.size.height + self.tabBar.frame.size.height - 20.0;
}
else
{
tabBarControllerFrame.size.height = screenSize.size.height + self.tabBar.frame.size.height ;
}
if (animated) {
[UIView animateWithDuration:0.2 animations:^{
[self.view setFrame:tabBarControllerFrame];
} completion:^(BOOL finished) {
}];
}
else
[self.view setFrame:tabBarControllerFrame];
}
- (void)showTabBarAnimated:(BOOL)animated {
CGRect statusbarFrame = [UIApplication sharedApplication].statusBarFrame;
CGRect tabBarControllerFrame = self.view.frame;
if (statusbarFrame.size.height>20)
{
tabBarControllerFrame.size.height = screenSize.size.height - 20.0;
}
else
{
tabBarControllerFrame.size.height = screenSize.size.height ;
}
if (animated) {
[UIView animateWithDuration:0.2 animations:^{
[self.view setFrame:tabBarControllerFrame];
} completion:^(BOOL finished) {
}];
}
else
[self.view setFrame:tabBarControllerFrame];
}
@end
Note: use statusbarFrame
is used when hotspotor callis ON so tabbar would not cut down.
注意:statusbarFrame
当热点或通话打开时使用,因此标签栏不会减少。
Now Import category in which you class you want to use methods and just call below methods to hide or show tabbar.
现在导入您要使用方法的类别,只需调用下面的方法来隐藏或显示标签栏。
[self.tabBarController hideTabBarAnimated:YES];
[self.tabBarController showTabBarAnimated:YES];
Hope this Helps.
希望这可以帮助。