自动布局和通话状态栏

Rud*_*vič 15 uiviewcontroller ios uistatusbar autolayout

我想询问有关自动布局和通话状态栏的信息.这是一个简单的场景,演示了我的问题:

  1. 创建启用了"使用故事板"的项目
  2. 添加"View Controller"并启用其"是初始视图控制器"
  3. 将控制器视图的背景颜色设置为红色
  4. 将"表视图"添加到控制器的视图中

表视图应具有4个布局约束(前导,顶部,尾部,底部)到Superview,常量设置为0.

现在,当我在模拟器中运行此应用程序并按+时,T我可以看到红色背景,而在通话状态栏动画.是否有可能摆脱这个故障?

her*_*rzi 8

(由于缺乏声誉而使用答案而不是评论,抱歉.)

我也遇到了这个问题并试图解决上面提到的解决方案:它对我不起作用.

所以我创建了一个包含示例代码的存储库来公开原始海报的问题.这些场景有示例应用程序:

  1. 自定义视图控制器是窗口的根视图控制器,
  2. 自定义视图控制器是UINavigationController的子代,它是窗口的根视图控制器,
  3. 自定义视图控制器是UITabBarController的子代,它是窗口的根视图控制器和
  4. Custom View Controller是UINavigationController的子代,它是UITabBarController的子代,UITabBarController是窗口的根视图控制器.

事实证明,当自定义视图控制器是UINavigationController的子代(案例2和4)时,CEarwood解决方案实际上有效.Hoewever,它在案例1和3中不起作用.

我希望这些信息有用.


CEa*_*ood 7

对于纯粹的自动布局答案,您可以获得对底部约束的引用,并在收到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
Run Code Online (Sandbox Code Playgroud)