点击以切换全屏 - 不透明tabBar的问题

时间:2014-09-27 18:03:16

标签: ios objective-c uitabbar

我试图通过隐藏statusBar,navigationBar和tabBar,在单个视图中点击用户点击“全屏”应用程序。

我可以隐藏并显示navigationBar和状态栏,但是我在隐藏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了很多,甚至在这里查了一下,但我找不到任何可以帮助我的东西。

视觉上这就是我想要实现的目标

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

TL; DR

我想让我的应用程序全屏显示,我想知道如何删除tabBar在隐藏时留下的空白点。

提前致谢。

编辑1

我在Sebastian Keller中按this question的回答,空白的tabBar现在已隐藏但动画有点大而且不顺畅。

编辑2

创建虚拟项目后,我重新构建了我的故事板,我注意到问题是,当tabBar设置为不透明时,它会留下空白栏。当它设置为半透明时,这不适用。

这是dummy project to demonstrate the issue

4 个答案:

答案 0 :(得分:2)

这就是我遇到那个黑条时解决它的方法。动画应该是平滑的。 此代码使用了您可以在此处找到的优秀 FrameAccessor 类:https://github.com/AlexDenisov/FrameAccessor

所以不要这样:

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

我们可以这样做:

view.x = 15;

这会容易得多。子类UITabBar控制器并在UITabBar Controller自定义类中选择它。

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

答案 1 :(得分: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;
}

这会自动拉伸所包含的控制器&#39;很好地看待。

代码here

答案 2 :(得分:0)

我最终使用the answer I've linked中建议的方法。经过一些调整,它正在发挥作用。

答案 3 :(得分:-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