ios 在 Ios7 中隐藏 tabBar 显示黑条
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19225450/
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
Hiding tabBar showing black bar in Ios7
提问by user2681789
I am using this code to hide the TabBar:
我正在使用此代码来隐藏 TabBar:
self.tabBarController.tabBar.hidden=YES;
I am hiding tabBarController in my project.but it showing black bar in bottom of the view in Ios7.When i go back to the same view it is looking good.any help will be appreciated.
我在我的项目中隐藏了 tabBarController。但它在 Ios7 的视图底部显示黑条。当我回到同一个视图时,它看起来很好。任何帮助将不胜感激。
回答by CTiPKA
NOTE: It is solution for iOS6 and 7 only.
注意:仅适用于 iOS6 和 7 的解决方案。
In iOS 7 to extend clickable area and hide black bar on place of hidden UITabBar you should enable 'Extend Edges - Under Opaque Bars' option for you UIViewController.
在 iOS 7 中,要扩展可点击区域并在隐藏的 UITabBar 位置隐藏黑条,您应该为您的 UIViewController 启用“扩展边缘 - 在不透明条下”选项。
Or you can set this property programmatically:
或者您可以以编程方式设置此属性:
[self setExtendedLayoutIncludesOpaqueBars:YES]
[self setExtendedLayoutIncludesOpaqueBars:YES]
Here is example of code that hide or move TabBar for iOS 6/7:
以下是为 iOS 6/7 隐藏或移动 TabBar 的代码示例:
UITabBarController *bar = [self tabBarController];
if ([self respondsToSelector:@selector(setExtendedLayoutIncludesOpaqueBars:)]) {
//iOS 7 - hide by property
NSLog(@"iOS 7");
[self setExtendedLayoutIncludesOpaqueBars:YES];
bar.tabBar.hidden = YES;
} else {
//iOS 6 - move TabBar off screen
NSLog(@"iOS 6");
CGRect screenRect = [[UIScreen mainScreen] bounds];
float height = screenRect.size.height;
[self moveTabBarToPosition:height];
}
//Moving the tab bar and its subviews offscreen so that top is at position y
-(void)moveTabBarToPosition:(int)y {
self.tabBarController.tabBar.frame = CGRectMake(self.tabBarController.tabBar.frame.origin.x, y, self.tabBarController.tabBar.frame.size.width, self.tabBarController.tabBar.frame.size.height);
for(UIView *view in self.tabBarController.view.subviews) {
if ([view isKindOfClass:[UITabBar class]]) {
[view setFrame:CGRectMake(view.frame.origin.x, y, view.frame.size.width, view.frame.size.height)];
} else {
[view setFrame:CGRectMake(view.frame.origin.x, view.frame.origin.y, view.frame.size.width, y)];
view.backgroundColor = [UIColor blackColor];
}
}
}
Function to moving the Tab Bar offscreen got from this post.
将 Tab Bar 移出屏幕的功能从这篇文章中得到。
回答by PSsam
Next code works for me
下一个代码对我有用
- (void)showTabBar {
[self.tabBar setTranslucent:NO];
[self.tabBar setHidden:NO];
}
- (void)hideTabBar {
[self.tabBar setTranslucent:YES];
[self.tabBar setHidden:YES];
}
回答by Tea
Try this:
尝试这个:
- (BOOL)hidesBottomBarWhenPushed {
return YES;
}
回答by Tea
I had some trouble while using a UINavigationController:
我在使用 UINavigationController 时遇到了一些麻烦:
Here's my solution that works for iOS 7 AND UINavigationControllers:
这是我适用于 iOS 7 和 UINavigationControllers 的解决方案:
HeaderFile
头文件
@interface UITabBarController (HideTabBar)
- (void)setHideTabBar:(BOOL)hide animated:(BOOL)animated;
@end
Implementation
执行
#import "UITabBarController+HideTabBar.h"
@implementation UITabBarController (HideTabBar)
- (void)setHideTabBar:(BOOL)hide animated:(BOOL)animated {
UIViewController *selectedViewController = self.selectedViewController;
/**
* If the selectedViewController is a UINavigationController, get the visibleViewController.
* - setEdgesForExtendedLayout won't work with the UINavigationBarController itself.
* - setExtendedLayoutIncludesOpaqueBars won't work with the UINavigationBarController itself.
*/
if ([selectedViewController isKindOfClass:[UINavigationController class]])
selectedViewController = ((UINavigationController *)selectedViewController).visibleViewController;
__weak __typeof(self) weakSelf = self;
void (^animations)(void) = ^{
selectedViewController.edgesForExtendedLayout = UIRectEdgeAll;
[selectedViewController setExtendedLayoutIncludesOpaqueBars:hide];
weakSelf.tabBar.hidden = hide;
/**
* Just in case we have a navigationController, call layoutSubviews in order to resize the selectedViewController
*/
[selectedViewController.navigationController.view layoutSubviews];
};
[UIView animateWithDuration:animated ? UINavigationControllerHideShowBarDuration : 0 animations:animations];
}
@end
Thanks to Vadim Trulyaevfor pointing out the Extend Edges - Under Opaque Barsflag!
感谢Vadim Trulyaev指出Extend Edges - Under Opaque Bars标志!
回答by Will
One line Swift 3 answer.
一行 Swift 3 答案。
Put the following in your UIViewController subclass:
将以下内容放入您的 UIViewController 子类中:
override var hidesBottomBarWhenPushed: Bool { get { return true } set { self.hidesBottomBarWhenPushed = newValue }}
回答by Haroldo Gondim
Set true
the property hidesBottomBarWhenPushed
in the controller to hide.
将控制器中true
的属性设置hidesBottomBarWhenPushed
为隐藏。
For hide, all your controllers put into prepare for segue
为了隐藏,你所有的控制器都放入 prepare for segue
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
segue.destination.hidesBottomBarWhenPushed = true
}
回答by smileBot
In addition to the other excellent suggestions the following suggestion might help someone out. Try setting your tabbar to hidden in awakeFromNib instead of later in the lifecycle. I found that the hidden tabbar was flashing black on segue and this fixed it for me.
除了其他出色的建议之外,以下建议可能会对某人有所帮助。尝试将标签栏设置为隐藏在awakeFromNib 中,而不是在生命周期的后期。我发现隐藏的标签栏在 segue 上闪烁黑色,这为我修复了它。
- (void)awakeFromNib
{
[super awakeFromNib];
self.tabBarController.tabBar.hidden = YES;
}
- (void)awakeFromNib
{
[super awakeFromNib];
self.tabBarController.tabBar.hidden = YES;
}
回答by user2681789
To showTabbar:
- (void)showTabBar:(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, 521, view.frame.size.width, view.frame.size.height)];
}
else
{
[view setFrame:CGRectMake(view.frame.origin.x, view.frame.origin.y, view.frame.size.width, 521)];
}
}
// [UIView commitAnimations];
}
To hide Tabbar:
- (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, 568, view.frame.size.width, view.frame.size.height)];
}
else
{
[view setFrame:CGRectMake(view.frame.origin.x, view.frame.origin.y, view.frame.size.width, 568)];
}
}
//[UIView commitAnimations];
}
回答by Ben Flynn
I spent a long time battling this, trying to place a responsive button at the bottom of table view. I am not using auto-layout. I found two main differences between iOS 6 and 7:
我花了很长时间来解决这个问题,试图在表格视图的底部放置一个响应式按钮。我没有使用自动布局。我发现 iOS 6 和 7 之间有两个主要区别:
On iOS7, when the tab bar is animated out, the view of the root view controller does not extend into the area where the tab bar was; it needs to be resized.
On iOS7, only the view of type UITabBar needs to be animated off and on the screen.
在iOS7上,当标签栏被动画化时,根视图控制器的视图不会延伸到标签栏所在的区域;它需要调整大小。
在 iOS7 上,只有 UITabBar 类型的视图需要在屏幕上和屏幕上进行动画处理。
A further issue with point 1 is that if, in iOS7, you extend a child view of your visible view controllers main view over the space left behind by the tab view, it won't be interactable unless the main view is extended as well. With that in mind, I used the following code:
第 1 点的另一个问题是,如果在 iOS7 中,您将可见视图控制器主视图的子视图扩展到选项卡视图留下的空间上,则除非主视图也被扩展,否则它将无法交互。考虑到这一点,我使用了以下代码:
Hide tab bar (reverse the math so show it):
隐藏标签栏(颠倒数学以显示它):
[UIView animateWithDuration:kHideTabBarAnimationDuration animations:^{
for(UIView *view in self.tabBarController.view.subviews)
{
if([view isKindOfClass:[UITabBar class]])
{
[view setFrame:CGRectMake(view.frame.origin.x, view.frame.origin.y + view.frame.size.height, view.frame.size.width, view.frame.size.height)];
}
else
{
if (![MYDeviceUtility systemVersionGreaterThanOrEqualTo:@"7.0"])
{
[view setFrame:CGRectMake(view.frame.origin.x, view.frame.origin.y, view.frame.size.width, view.frame.size.height + self.tabBarController.tabBar.frame.size.height)];
}
}
}
} completion:nil];
Adjust the main view when hiding tab bar:
隐藏标签栏时调整主视图:
// Expand view into the tab bar space
if ([MYDeviceUtility systemVersionGreaterThanOrEqualTo:@"7.0"])
{
CGRect frame = self.view.frame;
self.view.frame = CGRectMake(frame.origin.x,
frame.origin.y,
frame.size.width,
frame.size.height + tabBarHeight);
}
Adjust the main view when revealing tab bar:
显示标签栏时调整主视图:
[UIView animateWithDuration:kHideTabBarAnimationDuration delay:0.0f options:UIViewAnimationOptionCurveEaseIn animations:^{
// Create space for the tab bar
if ([MYDeviceUtility systemVersionGreaterThanOrEqualTo:@"7.0"])
{
CGRect frame = self.view.frame;
self.view.frame = CGRectMake(frame.origin.x,
frame.origin.y,
frame.size.width,
frame.size.height - tabBarHeight);
}
} completion:nil];
Note that I don't animate the main view expansion when hiding the tab bar, this looks natural since the expansion happens behind the tab bar.
请注意,在隐藏选项卡栏时,我不会为主视图扩展设置动画,这看起来很自然,因为扩展发生在选项卡栏后面。
Also note
还要注意
In iOS 7, if you rotate from portrait to landscape while the tab bar is hidden, the black box reappears. I solved this by animating the tab bar back onto the screen before the rotation animation (which was good enough for what I'm working on).
在 iOS 7 中,如果在隐藏标签栏的情况下从纵向旋转到横向,黑框会重新出现。我通过在旋转动画之前将标签栏动画回到屏幕上来解决这个问题(这对于我正在做的工作来说已经足够了)。
回答by maicon.peixinho
Based on solution of @Vadim Trulyaev, i created a simple usage:
基于@Vadim Trulyaev 的解决方案,我创建了一个简单的用法:
UITabBarController+HideTabBar.h
UITabBarController+HideTabBar.h
@interface UITabBarController (Additions)
- (void)setTabBarHidden:(BOOL)hidden myClass:(UIViewController *)myClass;
@end
UITabBarController+HideTabBar.m
UITabBarController+HideTabBar.m
#import "UITabBarController+HideTabBar.h"
@implementation UITabBarController (HideTabBar)
- (void)setTabBarHidden:(BOOL)hidden myClass:(UIViewController *)myClass{
if ([myClass respondsToSelector:@selector(setExtendedLayoutIncludesOpaqueBars:)]) {
//iOS 7 - hide by property
NSLog(@"iOS 7");
[myClass setExtendedLayoutIncludesOpaqueBars:hidden];
self.tabBar.hidden = hidden;
} else {
//iOS 6 - move TabBar off screen
NSLog(@"iOS 6");
CGRect screenRect = [[UIScreen mainScreen] bounds];
float height = screenRect.size.height;
if(hidden){
[self moveTabBarToPosition:height];
}else{
[self moveTabBarToPosition:height - self.tabBar.frame.size.height];
}
}
}
//Moving the tab bar and its subviews offscreen so that top is at position y
-(void)moveTabBarToPosition:(int)y {
self.tabBar.frame = CGRectMake(self.tabBarController.tabBar.frame.origin.x, y, self.tabBar.frame.size.width, self.tabBar.frame.size.height);
for(UIView *view in self.view.subviews) {
if ([view isKindOfClass:[UITabBar class]]) {
[view setFrame:CGRectMake(view.frame.origin.x, y, view.frame.size.width, view.frame.size.height)];
} else {
NSLog(@"%f",view.frame.size.height);
[view setFrame:CGRectMake(view.frame.origin.x, view.frame.origin.y, view.frame.size.width, y)];
NSLog(@"%f",view.frame.size.height);
view.backgroundColor = [UIColor blackColor];
}
}
}
@end
How to use:
如何使用:
[[self tabBarController] setTabBarHidden:NO myClass:self];
BUT, in iOS6 i have some issue, when i go first time to ViewController1 where the tabbar is hidden everthing works fine, but if i go to a ViewController2 that show the tab bar and back to ViewController1 that the tab bar must be hidden, the black space show up. Anyone can help me?!
但是,在 iOS6 中,我遇到了一些问题,当我第一次进入隐藏标签栏的 ViewController1 时,一切正常,但是如果我转到显示标签栏的 ViewController2 并返回到必须隐藏标签栏的 ViewController1,则黑色空间出现。任何人都可以帮助我吗?!
Thanks!
谢谢!