我正在使用这段代码来隐藏TabBar:
self.tabBarController.tabBar.hidden=YES;
我在我的项目中隐藏了tabBarController,但在iOS7中,在视图底部显示黑色条。当我返回到相同的视图时,它看起来很好。任何帮助将不胜感激。
我正在使用这段代码来隐藏TabBar:
self.tabBarController.tabBar.hidden=YES;
我在我的项目中隐藏了tabBarController,但在iOS7中,在视图底部显示黑色条。当我返回到相同的视图时,它看起来很好。任何帮助将不胜感激。
注意:这是针对iOS 6和7的解决方案。
在iOS 7中,为了扩展可点击区域并隐藏隐藏的UITabBar位置上的黑色条,您应该为您的UIViewController启用“扩展边缘 - 在不透明的栏下”选项。
或者,您可以编程设置此属性:
[self setExtendedLayoutIncludesOpaqueBars:YES]
这是一个隐藏或移动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];
}
}
}
从这篇文章中获取的将选项卡栏移动到屏幕外的函数。
以下代码适用于我
- (void)showTabBar {
[self.tabBar setTranslucent:NO];
[self.tabBar setHidden:NO];
}
- (void)hideTabBar {
[self.tabBar setTranslucent:YES];
[self.tabBar setHidden:YES];
}
试试这个:
- (BOOL)hidesBottomBarWhenPushed {
return YES;
}
我在使用UINavigationController时遇到了一些问题:
这是适用于iOS 7和UINavigationControllers的解决方案:
头文件
@interface UITabBarController (HideTabBar)
- (void)setHideTabBar:(BOOL)hide animated:(BOOL)animated;
@end
实现
#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
感谢Vadim Trulyaev指出的在不透明栏下延伸边缘标志!
一行Swift 3代码。
将以下内容放入你的UIViewController子类中:
override var hidesBottomBarWhenPushed: Bool { get { return true } set { self.hidesBottomBarWhenPushed = newValue }}
将控制器中的属性hidesBottomBarWhenPushed
设置为true
以进行隐藏。
要进行隐藏,需要将所有控制器放入prepare for segue
中。
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
segue.destination.hidesBottomBarWhenPushed = true
}
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];
}
我花了很长时间来解决这个问题,试图在表视图底部放置一个响应按钮。我没有使用自动布局。我发现iOS 6和7之间有两个主要区别:
iOS7中,当标签栏退出动画时,根视图控制器的视图不会延伸到标签栏所在的区域;它需要被重新调整大小。
iOS7只需要对UITabBar类型的视图进行屏幕内外的动画操作。
另一个问题是,如果在iOS7中,您将可见视图控制器的子视图扩展到标签视图留下的空间上,除非也扩展主视图,否则它不会产生交互作用。考虑到这一点,我使用了以下代码:
隐藏标签栏(反转数学计算以显示它):
[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];
隐藏选项卡栏时调整主视图:
// 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);
}
显示选项卡栏时调整主视图:
[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];
基于 @Vadim Trulyaev 的解决方案,我创建了一个简单的用法:
UITabBarController+HideTabBar.h
@interface UITabBarController (Additions)
- (void)setTabBarHidden:(BOOL)hidden myClass:(UIViewController *)myClass;
@end
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
如何使用:
[[self tabBarController] setTabBarHidden:NO myClass:self];
但是,在iOS6中我遇到了一些问题。当我第一次进入隐藏标签栏的ViewController1时,一切都运行良好,但是如果我进入一个显示标签栏的ViewController2,然后返回到需要隐藏标签栏的ViewController1时,黑色的空白会出现。有人可以帮助我吗?
谢谢!