Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to position view below green bar during phone call?

How do I make my view appear below the green bar during a phone call? right now my app is being partially covered by the green bar during a phone call.

like image 526
Sheehan Alam Avatar asked Jul 21 '10 21:07

Sheehan Alam


3 Answers

You can find out when this is about to happen using the following UIApplicationDelegate methods:

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

Alternatively, you can register for a notification in your UIViewController subclass:

- (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.
}
like image 104
Art Gillespie Avatar answered Nov 15 '22 18:11

Art Gillespie


If you're using Interface Builder, make sure your autoresize masks are set so that it resizes the views instead of just having them sit there in the same position. You can use the Toggle In-Call Status Bar option under Hardware in the Simulator to test it.

If you're not using IB, you can set up the autoresize masks in code.

If you're not using views, you'll need to resize your graphics when you get the appropriate notifications.

like image 44
lucius Avatar answered Nov 15 '22 19:11

lucius


Note that the suggestions above also fire when the device is rotated. If you just want to find when the status bar has grown in height due to an incoming/ongoing phone call. I found the following code to work:

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];
}
like image 43
user1208489 Avatar answered Nov 15 '22 19:11

user1208489