首页 > 解决方案 > 如何在 iOS 的状态栏和导航栏顶部添加子视图?

问题描述

我正在尝试在状态栏和导航栏之间添加一个子视图,所以我设置了 y 位置 = 0,然后我减小了 UIApplication Keywindow 的大小,以便它可以释放顶部的空间。

但是状态栏看起来很奇怪,它也被转移了。 在此处输入图像描述

导航栏向下移动,没关系。但我也需要显示正确的状态栏。这是我的代码。

    float yPosition = 0;
    CGRect fr = CGRectMake(0, yPosition, UIScreen.mainScreen.bounds.size.width, 40);
    
    runningCallView.frame = fr;
    
    [UIApplication sharedApplication].keyWindow.frame = CGRectMake(0, 40, UIScreen.mainScreen.bounds.size.width, UIScreen.mainScreen.bounds.size.height-40);
    [[UIApplication sharedApplication].keyWindow.rootViewController.view addSubview:runningCallView];```

Can anyone please help on this. Thank you.

标签: iosobjective-cnavigationbarsubviewuiwindow

解决方案


我已经解决了这个问题。现在,我没有向下移动 keyWindow,而是向下移动了 rootviewController。

[UIApplication sharedApplication].keyWindow.rootViewController.view.frame = CGRectMake(0, shiftDownwards, UIScreen.mainScreen.bounds.size.width, UIScreen.mainScreen.bounds.size.height-shiftDownwards);

并添加了一个状态栏子视图以在顶部显示一个假子视图。UiWindow 也需要是子类来检测额外添加的视图上的触摸。

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event

推荐阅读