【问题标题】:iOS 9 extended status bar bug?iOS 9 扩展状态栏错误?
【发布时间】:2016-09-12 14:48:44
【问题描述】:

我的应用出现问题,应用顶部有一个黑条,我的整个视图从顶部向下推了 20 点。

这里有一个非常简短的示例项目,它重现了该错误:https://github.com/sbiermanlytle/iOS-status-bar-bug/tree/master

复制:

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

如何去掉黑条?

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

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

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

【问题讨论】:

  • 在 SecondViewController 中的 buttonPushed 更改 nav.modalTransitionStyle = UIModalPresentationPopover;到 nav.modalPresentationStyle = UIModalPresentationOverCurrentContext;
  • 仍然看到黑条 - 您是否在扩展状态栏处于活动状态时进行了测试?
  • 是的,我在设备和模拟器中测试过(command + Y)。

标签: ios cocoa-touch ios9 uistatusbar


【解决方案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];
}

或者你可以把有问题的控制器的背景变成黑色:)

【讨论】:

  • 我无法使用已弃用的 API b/c 我之前遇到了问题,不得不切换到新的。此外,我无法将屏幕变为黑色 b/c 实际上有一个图像和一个底部栏被推离屏幕。不过,window hack 在演示应用程序中完美运行!将不得不看看它是否在其他地方弄乱了布局
  • 只关心 iOS 版本。视图层次结构的这一特定细节没有记录在案,可能有一天会改变,也可能不会改变,并有可能破坏事物。我建议 Apple 修复它后立即回退到“正常”实施。
【解决方案2】:

最简单的调整

@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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-03-25
    • 2016-01-03
    • 2015-11-01
    • 1970-01-01
    • 2015-12-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多