如何在通话期间将视图放在绿色栏下面?

She*_*lam 11 iphone cocoa-touch objective-c interface-builder uiview

如何在通话期间让我的视图显示在绿色栏下方?现在,我的应用程序在打电话时被绿色栏部分覆盖.

Art*_*pie 24

您可以使用以下UIApplicationDelegate方法找出何时发生这种情况:

- (void)application:(UIApplication *)application willChangeStatusBarFrame:(CGRect)newStatusBarFrame {
    NSLog(@"willChangeStatusBarFrame : newSize %f, %f", newStatusBarFrame.size.width, newStatusBarFrame.size.height);
}

- (void)application:(UIApplication *)application didChangeStatusBarFrame:(CGRect)newStatusBarFrame {
    NSLog(@"didChangeStatusBarFrame : newSize %f, %f", newStatusBarFrame.size.width, newStatusBarFrame.size.height);
}
Run Code Online (Sandbox Code Playgroud)

或者,您可以在UIViewController子类中注册通知:

- (void)viewDidLoad {
    [super viewDidLoad];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(statusBarFrameChanged:) name:UIApplicationDidChangeStatusBarFrameNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(statusBarFrameWillChange:) name:UIApplicationWillChangeStatusBarFrameNotification object:nil];
}

- (void)statusBarFrameWillChange:(NSNotification*)notification {
    NSValue* rectValue = [[notification userInfo] valueForKey:UIApplicationStatusBarFrameUserInfoKey];
    CGRect newFrame;
    [rectValue getValue:&newFrame];
    NSLog(@"statusBarFrameWillChange: newSize %f, %f", newFrame.size.width, newFrame.size.height);
    // Move your view here ...
}

- (void)statusBarFrameChanged:(NSNotification*)notification {
    NSValue* rectValue = [[notification userInfo] valueForKey:UIApplicationStatusBarFrameUserInfoKey];
    CGRect oldFrame;
    [rectValue getValue:&oldFrame];
    NSLog(@"statusBarFrameChanged: oldSize %f, %f", oldFrame.size.width, oldFrame.size.height);
    // ... or here, whichever makes the most sense for your app.
}
Run Code Online (Sandbox Code Playgroud)

  • 您甚至可以像这样使用UIKit扩展:CGRect newFrame = [rectValue CGRectValue]; - 无论哪种方式! (2认同)

luc*_*ius 7

如果您正在使用Interface Builder,请确保设置自动调整大小的蒙版,以便调整视图大小,而不是让它们位于相同位置.您可以使用模拟器中硬件下的Toggle In-Call Status Bar选项来测试它.

如果您不使用IB,则可以在代码中设置自动调整大小的掩码.

如果您没有使用视图,则需要在获得相应通知时调整图形大小.


use*_*489 6

请注意,上述建议也会在设备旋转时触发.如果您只是想查找状态栏由于来电/正在进行的电话而增加的高度.我找到了以下代码:

AppDelegate.m

- (void)application:(UIApplication *)application didChangeStatusBarFrame:(CGRect)oldStatusBarFrame
{
    float adjustment = 0;

    if ([UIApplication sharedApplication].statusBarFrame.size.height == 40) 
    {
        adjustment = -20;
    }
    else if  ([UIApplication sharedApplication].statusBarFrame.size.height == 20 && oldStatusBarFrame.size.height == 40)
    {
        adjustment = 20;
    }
    else 
    {
        return;
    }

    CGFloat duration = [UIApplication sharedApplication].statusBarOrientationAnimationDuration;
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:duration];

    {YOUR VIEW Controller - remove .view if a view}.view.frame = CGRectMake({YOUR VIEW Controller}.view.frame.origin.x, {YOUR VIEW Controller}.view.frame.origin.y + adjustment, {YOUR VIEW Controller}.view.frame.size.width, {YOUR VIEW Controller}.view.frame.size.height);

    [UIView commitAnimations];
}
Run Code Online (Sandbox Code Playgroud)