2014-09-27 39 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