iOS 9扩展状态栏错误?

时间:2016-05-16 17:59:00

标签: ios cocoa-touch ios9 uistatusbar

我的应用中遇到问题,应用顶部有一个黑条,我的整个视图从顶部向下推20分。

这是一个非常简短的示例项目,可以重现错误:https://github.com/sbiermanlytle/iOS-status-bar-bug/tree/master

重现:

  1. 激活扩展状态栏(在Google地图上启动路线或开启热点)
  2. 启动演示应用
  3. 导航至第3个视图
  4. 回到第二个视图,你会看到顶部的黑条
  5. 如何摆脱黑条?

    以下是示例应用的完整说明:

    有一系列3个视图控制器,1个启动2个,UIViewControllerBasedStatusBarAppearance设置为YES,第1个和第3个视图控制器隐藏状态栏,第2个显示状态栏。

    当你启动第二个视图和第三个视图时,一切都显示正常,但当你关闭第三个视图时,第二个视图顶部有一个黑色条不会消失。

2 个答案:

答案 0 :(得分:1)

看起来像iOS bug

我不知道这方面的美妙方式,但这些确实有效:

使用已弃用的API

将Info.plist中的View controller-based status bar appearance条目更改为NO。将这样的代码添加到视图控制器的所有(或公共超类,希望你有一个;)):

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [[UIApplication sharedApplication] setStatusBarHidden:[self prefersStatusBarHidden]];
}

使用丑陋的调整

通过在导致条件时手动更新失败的系统视图的帧。这可能会或可能不会破坏测试应用程序之外的内容。

UIViewController+TheTweak.h

#import <UIKit/UIKit.h>

@interface UIViewController (TheTweak)

- (void)transitionViewForceLayoutTweak;

@end

UIViewController+TheTweak.m介意FIXME评论

#import "UIViewController+TheTweak.h"
#import "UIView+TheTweak.h"

@implementation UIViewController (TheTweak)

- (void)transitionViewForceLayoutTweak {
    UIViewController *presenting = [self presentingViewController];
    if (([self presentedViewController] != nil) && ([self presentingViewController] != nil)) {
        if ([self prefersStatusBarHidden]) {

            NSUInteger howDeepDownTheStack = 0;
            do {
                ++howDeepDownTheStack;
                presenting = [presenting presentingViewController];
            } while (presenting != nil);

            //FIXME: replace with a reliable way to get window throughout whole app, without assuming it is the 'key' one. depends on your app's specifics
            UIWindow *window = [[UIApplication sharedApplication] keyWindow];
            [window forceLayoutTransitionViewsToDepth:howDeepDownTheStack];
        }
    }
}

@end

UIView+TheTweak.h

#import <UIKit/UIKit.h>

@interface UIView (TheTweak)

- (void)forceLayoutTransitionViewsToDepth:(NSUInteger)depth;

@end

UIView+TheTweak.m

#import "UIView+TheTweak.h"

@implementation UIView (TheTweak)

- (void)forceLayoutTransitionViewsToDepth:(NSUInteger)depth {
    if (depth > 0) { //just in case
        for (UIView *childView in [self subviews]) {
            if ([NSStringFromClass([childView class]) isEqualToString:@"UITransitionView"]) {
                childView.frame = self.bounds;
                if (depth > 1) {
                    [childView forceLayoutTransitionViewsToDepth:(depth - 1)];
                }
            }
        }
    }
}

@end

现在,在每个视图控制器(或共同的超类)中:

#import "UIViewController+TheTweak.h"

... // whatever goes here

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

    [self transitionViewForceLayoutTweak];
}

或者你可以将问题控制器黑色的背景变为:)

答案 1 :(得分:0)

最简单的调整

@interface SecondViewController ()

@property (assign, nonatomic) BOOL statusBarHidden;

@end

@implementation SecondViewController

- (void)viewWillAppear:(BOOL)animated {
  [super viewWillAppear:animated];
  self.statusBarHidden = [UIApplication sharedApplication].statusBarHidden; //store current state
}

- (void)viewDidAppear:(BOOL)animated {
  [super viewDidAppear:animated];
  self.statusBarHidden = YES;
  [UIView animateWithDuration:0.2f animations:^{
     [self setNeedsStatusBarAppearanceUpdate];
   }];
}

- (BOOL)prefersStatusBarHidden {
    return self.statusBarHidden;
}
@end