我的应用中遇到问题,应用顶部有一个黑条,我的整个视图从顶部向下推20分。
这是一个非常简短的示例项目,可以重现错误:https://github.com/sbiermanlytle/iOS-status-bar-bug/tree/master
重现:
如何摆脱黑条?
以下是示例应用的完整说明:
有一系列3个视图控制器,1个启动2个,UIViewControllerBasedStatusBarAppearance
设置为YES
,第1个和第3个视图控制器隐藏状态栏,第2个显示状态栏。
当你启动第二个视图和第三个视图时,一切都显示正常,但当你关闭第三个视图时,第二个视图顶部有一个黑色条不会消失。
答案 0 :(得分:1)
看起来像iOS bug。
我不知道这方面的美妙方式,但这些确实有效:
将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