Search code examples
iphoneobjective-cios6ios7

Programmatically set banner view to fix to bottom of screen


I have programmatically added a bannerView to my app. On iOS7 4" and 3.5" it works perfectly, however on iOS6, it moves the banner down by 20px, obviously because of the Status bar but I want the banner to always fit to the bottom of the screen, not fall 20 pixels off

My code is like this:

   [bannerView_ loadRequest:[GADRequest request]];


    [bannerView_ setFrame:CGRectMake([UIScreen mainScreen].bounds.size.width-bannerView_.frame.size.width,
                                     [UIScreen mainScreen].bounds.size.height-bannerView_.frame.size.height,
                                     bannerView_.frame.size.width,
                                     bannerView_.frame.size.height
                                     )];

Is there a way to programmatically change the deltas or something? Basically the banner needs to move 20px up on iOS6


Solution

  • UIApplication exposes statusBarFrame. That will give you the height of the status bar (normally, 20 points). You can then check if the user has iOS6 and offset the banner. Check by comparing NSFoundationVersionNumber <= NSFoundationVersionNumber_iOS_6_1.

    Try:

    [bannerView_ setFrame:CGRectMake([UIScreen mainScreen].bounds.size.width - bannerView_.frame.size.width, [UIScreen mainScreen].bounds.size.height - bannerView_.frame.size.height - (NSFoundationVersionNumber <= NSFoundationVersionNumber_iOS_6_1 ? [UIApplication sharedApplication].statusBarFrame.size.height : 0), bannerView_.frame.size.width, bannerView_.frame.size.height)];
    

    Keep in mind, if you want to support rotation, your task will get harder, as you'd have to position the view after rotation, and you'd need to use statusBarFrame.width in landscape.