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

时间:2010-07-21 21:26:36

标签: iphone objective-c cocoa-touch uiview interface-builder

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

4 个答案:

答案 0 :(得分: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);
}

或者,您可以在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.
}

答案 1 :(得分:7)

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

如果您没有使用IB,可以在代码中设置自动调整大小的掩码。

如果您没有使用视图,则需要在收到相应的通知时调整图形大小。

答案 2 :(得分: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];
}

答案 3 :(得分:0)

同意使用标签栏应用和iOS6 / 7,感谢user1208489' s answer

每个iOS的值完全不同且很奇怪。 ;业务

mainAppDelegate.m

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

#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 70000 // pour Xcode5/SDK7

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

#else // pour Xcode4/SDK6

if ([UIApplication sharedApplication].statusBarFrame.size.height == 40)
{
    vue = +20;
    hauteur = 1;
    barre = -1-20;
}
else
    if ([UIApplication sharedApplication].statusBarFrame.size.height == 20 && oldStatusBarFrame.size.height == 40)
    {
        vue = -20;
        hauteur = -1;
        barre = +1+20;
    }
    else return;

#endif

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

self.tabBarController.view.frame = CGRectMake(self.tabBarController.view.frame.origin.x, self.tabBarController.view.frame.origin.y + vue, self.tabBarController.view.frame.size.width, self.tabBarController.view.frame.size.height+hauteur);
self.tabBarController.tabBar.frame = CGRectMake(self.tabBarController.tabBar.frame.origin.x, self.tabBarController.tabBar.frame.origin.y + barre, self.tabBarController.tabBar.frame.size.width, self.tabBarController.tabBar.frame.size.height);

[UIView commitAnimations];
}

使用Xcode 4.6.3(4H1503)和Xcode 5.0.2(5A3005)在iPhone 4S 7.0.4(11B554a)上测试。