Iphone 打电话时如何将视图定位在绿色条下方?

Iphone 打电话时如何将视图定位在绿色条下方?,iphone,objective-c,cocoa-touch,uiview,interface-builder,Iphone,Objective C,Cocoa Touch,Uiview,Interface Builder,在打电话时,如何使我的视图显示在绿色栏下方?现在,我的应用程序在通话过程中部分被绿色条覆盖。您可以使用以下UIApplicationLegate方法了解何时会发生这种情况: - (void)application:(UIApplication *)application willChangeStatusBarFrame:(CGRect)newStatusBarFrame { NSLog(@"willChangeStatusBarFrame : newSize %f, %f", newSt

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

您可以使用以下UIApplicationLegate方法了解何时会发生这种情况:

- (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.
}

如果您使用的是Interface Builder,请确保设置了自动调整大小的遮罩,以便它可以调整视图的大小,而不是让它们位于相同的位置。您可以使用模拟器中“硬件”下的“调用状态栏切换”选项对其进行测试

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


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

请注意,当设备旋转时,上面的建议也会触发。如果您只想查找由于来电/正在通话而导致状态栏高度增加的时间。我发现以下代码可以工作:

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];
}
- (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];
}

Idem具有选项卡栏应用程序和iOS6/7,这要感谢的是

每个iOS的值都完全不同且怪异;oP

mainAppDelegate.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];
}
- (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)上进行测试。

您甚至可以使用UIKit扩展,如:CGRect newFrame=[rectValue CGRectValue];-不管怎样!我只想补充一点,NSLog和命名约定有点错误。statusBarFrameWillChange在statusBarFrameChanged之前调用,因此来自statusBarFrameWillChange的通知实际上是旧值而不是新值。反之亦然。