2014-09-27 41 views
0

我想通过隐藏statusBar,navigationBar和tabBar在一起,在用户点击一个视图中使我的应用程序“全屏”。点击切换全屏 - 问题与不透明tabBar

我可以隐藏并显示导航栏和状态栏的罚款,但我在隐藏tabBar时遇到一些问题。

这是它看起来像藏前:

而这个隐藏之后:

隐藏当,使用TabBar留下空白点,我已经试图隐瞒没有成功。

这是我目前使用

-(void)toggleBars:(UITapGestureRecognizer *)gesture{ 
    //Hide navigationBar  
    BOOL toggleNavigationBar = self.navigationController.navigationBarHidden; 
    [self.navigationController setNavigationBarHidden:!toggleNavigationBar animated:YES]; 


    //Hide tabBar - not hiding, leaving a black spot 
    BOOL toggleTabHidden = self.tabBarController.tabBar.hidden; 
    [self.tabBarController setTabBarHidden:!toggleTabHidden]; 

    //Hide statusBar 
    BOOL statusBarHidden = [UIApplication sharedApplication].statusBarHidden; 
    [[UIApplication sharedApplication] setStatusBarHidden:!statusBarHidden withAnimation:UIStatusBarAnimationSlide]; 
    if ([self respondsToSelector:@selector(setNeedsStatusBarAppearanceUpdate){ 
     [self setNeedsStatusBarAppearanceUpdate]; 
    } 
} 

我GOOGLE了很多,甚至在这里抬头对SO,但我没有找到任何可以帮助我的代码。

视觉这就是我想要达到

----------------     -------------- 
|navBar &statbar|    |    | 
|---------------     |    | 
|    |  tap  |    | 
| content  |  ----->  | content only |   
|    |    |in fullscreen | 
|    |    |    | 
|-------------- |    |    | 
| tabbar  |    |    | 
-------------     -------------- 

TL; DR

我想就我的水龙头全屏应用程序,我想知道如何删除TabBar隐藏时留下的空白点。

在此先感谢。

编辑1

我接着Sebastian Keller答案在this question,空白的TabBar隐时现,但该动画是一个有点大越野车和不光滑。

EDIT 2

创建一个虚拟项目之后,我重拍我的故事板,我注意到的问题是,当使用TabBar设置为不透明,它留下空白栏的后面。当它设置为半透明时,这不适用。

这是dummy project to demonstrate the issue

+0

您使用的是自动布局吗?我想我可能会为你解决问题。 – 2014-10-03 03:09:36

+0

是的,我正在使用Autolayout。 – Phillip 2014-10-03 08:48:30

回答

0

我最终使用了the answer I've linked中建议的方法。随着一些调整它正在工作。

+0

我认为这是对'layoutSubviews'的调用,它是关键 – 2014-10-06 13:05:55

-1

使用下面的代码,这几乎等同于你自己,我有没有问题重现您想要的效果:

- (void)viewDidLoad { 
    [super viewDidLoad]; 

    UITapGestureRecognizer *tapGR = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(toggleBars)]; 
    [self.view addGestureRecognizer:tapGR]; 
} 

- (void)toggleBars { 
    BOOL toggleNavigationBar = self.navigationController.navigationBarHidden; 
    [self.navigationController setNavigationBarHidden:!toggleNavigationBar animated:YES]; 

    BOOL toggleTabHidden = self.tabBarController.tabBar.hidden; 
    [self.tabBarController.tabBar setHidden:!toggleTabHidden]; 

    BOOL statusBarHidden = [UIApplication sharedApplication].statusBarHidden; 
    [[UIApplication sharedApplication] setStatusBarHidden:!statusBarHidden withAnimation:UIStatusBarAnimationSlide]; 
    if ([self respondsToSelector:@selector(setNeedsStatusBarAppearanceUpdate)]) { 
     [self setNeedsStatusBarAppearanceUpdate]; 
    } 
} 

视频演示: https://www.dropbox.com/s/5vzgfxc5f043lxy/hide.mov?dl=0

+0

我试过了,但不幸的是空白栏不断弹出:( – Phillip 2014-09-29 18:58:15

2

这是我如何解决它当遇到那黑色的酒吧。动画应该是平滑的。 此代码使用大FrameAccessor类,你可以在这里找到:https://github.com/AlexDenisov/FrameAccessor

所以不是这样的:

CGRect newFrame = view.frame; 
newFrame.origin.x = 15; 
view.frame = newFrame; 

我们可以这样做:

view.x = 15; 

这要容易得多。子类UITabBar控制器,并在您的UITabBar控制器自定义类中选择它。

enter image description here

SSTabBarController.h:

#import <UIKit/UIKit.h> 

@interface SSTabBarController : UITabBarController 
@property (assign) BOOL isTabBarOpen; 

-(void)showOrHideTabBar; 
- (void)hideTabBarWithAnimation:(BOOL)animated; 
- (void)showTabBarWithAnimation:(BOOL)animated; 

@end 

SSTabBarController.m:

#import "SSTabBarController.h" 
#import "FrameAccessor.h" 

#define IPHONE_HEIGHT [[UIScreen mainScreen] bounds].size.height 
#define TABBAR_HEIGHT 49 

@interface SSTabBarController() 

@end 

@implementation SSTabBarController 

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil 
{ 
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; 
    if (self) { 
     // Custom initialization 
    } 
    return self; 
} 


- (void)viewWillAppear:(BOOL)animated 
{ 
    [super viewWillAppear:animated]; 

} 
- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    //This part is optional. If you are using opaque tabbar you can mark extend edged in your `StoryBoard`. 
     /*UIView *tabBarBackround = [[UIView alloc]initWithFrame:CGRectMake(0, [UIScreen   mainScreen].bounds.size.height-TABBAR_HEIGHT, 320, TABBAR_HEIGHT)]; 
    tabBarBackround.backgroundColor =[UIColor colorWithRed:236/255.0 green:236/255.0 blue:236/255.0 alpha:1]; 
    [self.view addSubview:tabBarBackround]; 
    [self.view insertSubview:self.tabBar aboveSubview:tabBarBackround];*/ 

} 

- (void)didReceiveMemoryWarning 
{ 
    [super didReceiveMemoryWarning]; 
} 

-(void)showOrHideTabBar 
{ 
    if(_isTabBarOpen) 
    { 
     [self hideTabBarWithAnimation:YES]; 
    } 
    else 
    { 
     [self showTabBarWithAnimation:YES]; 
    } 
} 

- (void)showTabBarWithAnimation:(BOOL)animated 
{ 
    _isTabBarOpen = YES; 
    for(UIView *view in self.view.subviews) 
    { 
     if([view isKindOfClass:[UITabBar class]]) 
     { 
      if(animated) 
      { 
       [UIView animateWithDuration:0.4 animations:^() 
        { 
         view.y = IPHONE_HEIGHT - view.height; 
        } 
            completion:^(BOOL finished){}]; 
      } 
      else 
      { 
       view.y = IPHONE_HEIGHT - view.height; 
      } 
     } 
    } 
} 

- (void)hideTabBarWithAnimation:(BOOL)animated 
{ 
    _isTabBarOpen = NO; 
    for(UIView *view in self.view.subviews) 
    { 
     if([view isKindOfClass:[UITabBar class]]) 
     { 
      if(animated) 
      { 
       [UIView animateWithDuration:0.4 animations:^() 
        { 
         view.y = IPHONE_HEIGHT; 
        } 
            completion:^(BOOL finished){}]; 
      } 
      else 
      { 
       view.y = IPHONE_HEIGHT; 
      } 
     } 
    } 
} 
@end 

在任何其他ViewController

- (IBAction)hideTabbar:(UIButton *)sender 
{ 

    SSTabBarController *myTabBar = (SSTabBarController*)self.tabBarController; 
    [myTabBar showOrHideTabBar]; 
} 

如果使用不透明确保这是您UITabBarController和你ViewController检查:

enter image description here

这里有一个工作项目与上面你可以下载:http://bit.ly/10fSxkU

+0

什么是IPHONE_HEIGHT常量? – Phillip 2014-09-29 20:03:45

+0

那么,iPhone的高度:)。编辑我的答案 – Segev 2014-09-29 20:05:41

+0

是啊,无论如何,猜对了如此哈哈view.y产生一个错误。不应该像view.frame.origin.y? – Phillip 2014-09-29 20:08:26

2

我喜欢要隐藏标签栏,请按照以下步骤将UITabBarController.view拉下:tabBar不在屏幕外:

- (void)setTabBarHidden:(BOOL)hidden 
{ 
    CGRect frame = self.originalViewFrame; 
    if (hidden) 
    { 
     frame.size.height += self.tabBar.size.height; 
    } 
    self.view.frame = frame; 
} 

这会自动延伸包含控制器的视图很好。代码here