2010-07-21 9 views

Respuesta

7

Si' Usando Interface Builder, asegúrese de que sus máscaras de autoresize estén configuradas para que cambie el tamaño de las vistas en lugar de tenerlas allí en la misma posición. Puede usar la opción Alternar la barra de estado durante la llamada en Hardware en el simulador para probarla.

Si no está utilizando IB, puede configurar las máscaras de autoresize en el código.

Si no está utilizando vistas, tendrá que cambiar el tamaño de sus gráficos cuando reciba las notificaciones correspondientes.

23

Puede averiguar cuando este está a punto de suceder usando los siguientes métodos 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); 
} 

Como alternativa, puede registrarse para obtener una notificación en su subclase 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. 
} 
+2

Incluso puede usar las extensiones UIKit de la siguiente manera: CGRect newFrame = [rectValue CGRectValue]; - ¡de cualquier manera! –

+0

Solo quiero agregar que la convención de nomenclatura y NSLog es un poco incorrecta. Se llama a statusBarFrameWillChange antes de statusBarFrameChanged para que la notificación de statusBarFrameWillChange sea realmente el valor ANTIGUO y no el nuevo. Viceversa con el otro método. – micnguyen

5

Tenga en cuenta que las sugerencias anteriores también se activan cuando se gira el dispositivo. Si solo desea encontrar cuando la barra de estado ha crecido en altura debido a una llamada telefónica entrante/en curso. He encontrado el siguiente código para trabajar:

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

Idem con una barra de aplicación Tab y iOS 6/7, gracias a user1208489 's answer.

Los valores son completamente diferentes y extraños para cada iOS. ; OP

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

Probado en iPhone 4S 7.0.4 (11B554a) con Xcode 4.6.3 (4H1503) y Xcode 5.0.2 (5A3005).

Cuestiones relacionadas