我想问关于自动布局和通话状态栏的问题,以下是一个简单的场景来说明我的问题:
- 创建启用“故事板”的项目
- 添加“视图控制器”并启用其“Is Initial View Controller”
- 将控制器视图的背景色设置为红色
- 将“表格视图”添加到控制器视图中
该表格视图应具有 4 个布局约束(leading,top,trailing,bottom),常量设置为 0。
现在当我在模拟器中运行此应用程序并按下 ⌘ + T,我可以看到红色背景,同时呼叫状态栏正在进行动画。是否可能消除这个小故障?
我想问关于自动布局和通话状态栏的问题,以下是一个简单的场景来说明我的问题:
该表格视图应具有 4 个布局约束(leading,top,trailing,bottom),常量设置为 0。
现在当我在模拟器中运行此应用程序并按下 ⌘ + T,我可以看到红色背景,同时呼叫状态栏正在进行动画。是否可能消除这个小故障?
由于声望不足,无法评论,我也遇到了这个问题,并尝试了上面提出的解决方案: 对我没有用。
因此,我创建了一个存储库,其中包含示例代码以展示原始帖子的问题。这些情况下都有应用示例:
结果发现,来自CEarwood的解决方案实际上可以在自定义视图控制器是UINavigationController的子视图控制器(情况2和4)时工作… 但在情况1和3中不起作用。
希望这些信息对您有用。
如果只考虑使用Auto Layout,当收到UIApplicationWillChangeStatusBarFrameNotification通知时,您可以获取底部约束的引用并调整其常量,并在收到DidChange通知时将其返回0。以下是我使用的测试VC:
@interface CEViewController ()
@property (nonatomic, strong) IBOutlet NSLayoutConstraint *bottomConstraint;
@end
@implementation CEViewController
- (void)viewDidLoad {
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(statusBarFrameWillChange:) name:UIApplicationWillChangeStatusBarFrameNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(statusBarFrameDidChange:) name:UIApplicationDidChangeStatusBarFrameNotification object:nil];
}
- (void)statusBarFrameWillChange:(NSNotification *)note {
NSValue *newFrameValue = [note userInfo][UIApplicationStatusBarFrameUserInfoKey];
self.bottomConstraint.constant = newFrameValue.CGRectValue.size.height;
[self.view setNeedsLayout];
}
- (void)statusBarFrameDidChange:(NSNotification *)note {
self.bottomConstraint.constant = 0;
[self.view setNeedsLayout];
}
@end
这是屏幕大小调整的影响。
当通话状态栏出现时,视图会重新调整大小以适应启用通话状态栏的大小,然后随着状态栏的更改而向下移动。
在短暂的瞬间,表格视图下方的视图是可见的。您可以在表格视图下方添加一个视图,向下延伸到屏幕外,以覆盖背景颜色。
另一种方法是使用您的AppDelegate
,实现以下内容:
-application:willChangeStatusBarFrame:
将表视图调整大小以覆盖暴露的部分。然后,在调用application:didChangeStatusBarFrame:
时,将其调整回原始大小。