iOS 8-旋转使切换控件后,即使在纵向模式下,statusBar也消失 [英] iOS 8 - Rotation makes statusBar disappear even in portrait mode after toggling controls

查看:202
本文介绍了iOS 8-旋转使切换控件后,即使在纵向模式下,statusBar也消失的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

iOS 8中状态栏的新自动隐藏给我带来很多麻烦.

I'm having a lot of troubles with the new auto hiding of the status bar in iOS 8.

在我的应用程序中,我有一个视图,当用户单击一次时,导航栏和状态栏就会消失.

In my app, I've got a view in which when the user taps once, the navigation bar and the status bar disappear.

在横向时,状态栏会自动隐藏,对我来说很好.我仅在纵向模式下需要它.

When in landscape, the status bar hides by itself and it's fine to me. I only need it in portrait mode.

但是问题是,当设备处于横向状态且显示了该条时,当用户点按两次以切换该条(如此显示)并以纵向模式旋转该设备时,状态栏仍然处于隐藏状态.

But the problem is that when the device is landscape and the bar are shown, when the user taps twice to toggle the bar (so showing), and turns the device in portrait mode, the status bar is still hidden.

基本上,我需要能够隐藏statusBar而不干扰其在iOS 8上的自然行为,所以我来回顾一下情况:

Basically I need to be able to hide the statusBar without interfere with its natural behavior on iOS 8, so I recap the scenario:

  • 用户使用tabBar和NavigationBar和statusBar进入所述视图;
  • 在视图中轻按一次,条形消失
  • 用户旋转设备,statusBar不出现-确定,我想要这个
  • 用户再次点击以显示条形-状态栏仍处于隐藏状态,确定.
  • 用户从横向旋转到纵向,然后.
  • statusBar仍然隐藏-不好.

MRW>
(来源:

MRW >
(source: mshcdn.com)

我试图在willRotate上调整statusBar,但是却搞砸了,使statusBar在原本不应该的情况下可见.我正在使用的代码:

I've tried to adjust the statusBar on willRotate, but I made a mess in which the statusBar would be visible when it wasn't supposed to. Code I'm using:

- (BOOL)prefersStatusBarHidden
{
    return statusBarHidden;
}

-(void)toggleBars:(UITapGestureRecognizer *)gesture{
    CATransition *animation = [CATransition animation];
    animation.type = kCATransitionFromBottom;
    animation.subtype = kCATransitionFromTop;
    animation.duration = .2f;
    [animation setTimingFunction:[CAMediaTimingFunction functionWithName: kCAMediaTimingFunctionEaseInEaseOut]];


    BOOL toggleNavigationBar = self.navigationController.navigationBarHidden;
    [self.navigationController.navigationBar.layer addAnimation:animation forKey:nil];
    [self.navigationController setNavigationBarHidden:!toggleNavigationBar animated:YES];



    BOOL toggleTabHidden = self.tabBarController.tabBar.hidden;
    if(![[[NSUserDefaults standardUserDefaults] objectForKey:@"showTabBar"]isKindOfClass:[NSNull class]]){
        if([(NSNumber*)[[NSUserDefaults standardUserDefaults] objectForKey:@"showTabBar"]boolValue])
        {
            [self.tabBarController.tabBar.layer addAnimation:animation forKey:nil];
            [self.tabBarController setHideTabBar:!toggleTabHidden animated:YES];
        }
    }

    statusBarHidden = [UIApplication sharedApplication].statusBarHidden;
    [[UIApplication sharedApplication] setStatusBarHidden:!statusBarHidden withAnimation:UIStatusBarAnimationSlide];
    [self setNeedsStatusBarAppearanceUpdate];


    if (IS_IOS8){
        if (UIInterfaceOrientationIsLandscape(self.interfaceOrientation)){
            if (statusBarHidden){
                [[UIApplication sharedApplication] setStatusBarHidden:YES];
            }
        }
    }

}

我正在考虑设置一个标志,在该标志中,当位于风景中时statusBar被隐藏并且所有控件都在其中,旋转时会触发statusBar.显然没有成功.

I was thinking about setting a flag in which when the statusBar is hidden when in landscape and all the controls are there, on rotation it would trigger the statusBar. With no success, apparently..

任何帮助都受到赞赏.

推荐答案

您是否正在使用基于UIViewController的状态栏外观? 如果您实现prefersStatusBarHidden,我想您是

Are you using UIViewController-based status bar appearance? If you implement prefersStatusBarHidden I assume you are.

现在,

[[UIApplication sharedApplication] setStatusBarHidden:!statusBarHidden withAnimation: UIStatusBarAnimationSlide];

不应与基于UIViewController的状态栏外观一起使用.

is not supposed to work with UIViewController-based status bar appearance.

您只需要从prefersStatusBarHidden方法返回不同的值,然后调用setNeedsStatusBarAppearanceUpdate通知应用程序返回值已更改.

You need just to return different value from prefersStatusBarHidden method and call setNeedsStatusBarAppearanceUpdate to notify the app that returning value has changed.

因此,要更改状态栏的可见性,您应该这样做

So to change statusbar visibility you should just do

@property (nonatomic, assign) BOOL hideStatusBar;

- (BOOL)prefersStatusBarHidden 
{
    return self.hideStatusBar;
}

- (void)toggleBars:(UITapGestureRecognizer *)gesture 
{
    ... hide navbar and tabbar ...

    self.hideStatusBar = ! self.hideStatusBar;
    [self setNeedsStatusBarAppearanceUpdate];
}

就是这样!

这篇关于iOS 8-旋转使切换控件后,即使在纵向模式下,statusBar也消失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆