Question

Currently I use this to code for loading the iAD banners.

- (void)viewDidLoad
{
    [super viewDidLoad];

    adView = [[ADBannerView alloc] initWithFrame:CGRectZero];
    CGRect adFrame = adView.frame;
    adFrame.origin.y = self.view.frame.size.height;
    adView.frame = adFrame;

    [adView setAutoresizingMask:UIViewAutoresizingFlexibleWidth];

    [self.view addSubview:adView];
    adView.delegate=self;

    self.bannerIsVisible=NO;

}

- (void)bannerViewDidLoadAd:(ADBannerView *)banner
{
    if (!self.bannerIsVisible)
    {
        [UIView beginAnimations:@"animateAdBannerOn" context:NULL];

        CGRect adFrame = adView.frame;
        adFrame.origin.y = self.view.frame.size.height-adView.frame.size.height;
        adView.frame = adFrame;

        [UIView commitAnimations];
        self.bannerIsVisible = YES;
    }
}

- (void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error
{
    if (self.bannerIsVisible)
    {
        [UIView beginAnimations:@"animateAdBannerOff" context:NULL];

        CGRect adFrame = adView.frame;
        adFrame.origin.y = self.view.frame.size.height+adView.frame.size.height;
        adView.frame = adFrame;

        [UIView commitAnimations];
        self.bannerIsVisible = NO;
    }
}

The problem is that I want to auto set the iAD view to the bottom of the view also when the device orientation changes. I tried lots of things but none of thim are working.

Also why do I have to use notification center to check device orientations? DidRotateToInterfaceOrientation doesn't seem to work.

Was it helpful?

Solution

I may have understood this wrong, but if you set the following:

[adView setAutoresizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin];

This should mean that as your view gets bigger/smaller (as you rotate) it should increase/decrease the 'top' margin, therefore should stick to the bottom.

I may have understood this incorrectly though.. are you able to provide screenshots of what is currently happening if the above idea doesn't work?

OTHER TIPS

Try this in your viewDidLoad method:

CGRect adFrame = adView.frame;
adFrame.origin.y = self.view.frame.size.height-adView.frame.size.height;
adView.frame = adFrame;

It will set your iAd to the bottom of your view irrespective of the device type. Don't set the frame of iAd in the delegate method.

This solution is for iOS 6

Use notifications to listen for a device orientation change in the viewDidLoad method of your view controller:

UIDevice *device = [UIDevice currentDevice];
[device beginGeneratingDeviceOrientationNotifications];
NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
[nc addObserver:self selector:@selector(orientationChanged:)name:UIDeviceOrientationDidChangeNotification object:device];

UIInterfaceOrientation destOrientation = self.interfaceOrientation;

Then use an if statement in your viewController to listen for the change, and reposition the Ad Banner View when the orientation changes:

if (destOrientation == UIInterfaceOrientationPortrait || destOrientation == UIInterfaceOrientationPortraitUpsideDown) {

    ADBannerView *bannerView = (ADBannerView *)[self.view viewWithTag:615];
    CGRect myAdBanner = bnrAd.frame;
    myAdBanner.origin.x = 0;
    myAdBanner.origin.y = 410;
    bannerView.frame = myAdBanner;

} else {

    ADBannerView *bannerView = (ADBannerView *)[self.view viewWithTag:615];
    CGRect myAdBanner = bnrAd.frame;
    myAdBanner.origin.x = 0;
    myAdBanner.origin.y = 265;
    bannerView.frame = myAdBanner;
}

Don't forget to call the methods to load the ad into your view:

- (void) bannerViewDidLoadAd:(ADBannerView *)banner {
    bnrAd = _bannerView;
}

- (BOOL) bannerViewActionShouldBegin:(ADBannerView *)banner willLeaveApplication:(BOOL)willLeave {
    return YES;
}

see the code below and I am using this code in my iPad App that need to support iOS 6 and above:

in your .h file

#import <iAd/iAd.h>

@interface MyViewController : UIViewController <ADBannerViewDelegate> {

    ADBannerView *bannerView;

    BOOL bannerIsVisible;

}

in your .m file

- (void)viewDidLoad
{
    [super viewDidLoad];
    // detect device orientation change
    [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];

    if (SYSTEM_VERSION_LESS_THAN(@"7.0")) {

        [[NSNotificationCenter defaultCenter]
         addObserver:self selector:@selector(orientationChanged:)
         name:UIDeviceOrientationDidChangeNotification
         object:[UIDevice currentDevice]];

    }

    [self showiAds];

}

- (void) showiAds{

    bannerView = [[ADBannerView alloc] initWithFrame:CGRectZero];

    if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"7.0")) {

        UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];

        if(orientation == UIDeviceOrientationLandscapeLeft || orientation == UIDeviceOrientationLandscapeRight) {

            [bannerView setAutoresizingMask:UIViewAutoresizingFlexibleWidth |UIViewAutoresizingFlexibleBottomMargin];

        } else {

            [bannerView setAutoresizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin];

        }

    }

    if (SYSTEM_VERSION_LESS_THAN(@"7.0")) {

        [bannerView setAutoresizingMask:UIViewAutoresizingFlexibleWidth |UIViewAutoresizingFlexibleBottomMargin];

    }

    bannerView.delegate = self;

    bannerView.hidden = YES;

    [self.view addSubview:bannerView];

    bannerIsVisible = NO;

}

- (void)bannerViewDidLoadAd:(ADBannerView *)banner
{
    NSLog(@"bannerViewDidLoadAd");

    if (!bannerIsVisible) {

        bannerView.hidden = NO;

        [UIView beginAnimations:@"animateAdBannerOn" context:NULL];

        // banner is invisible now and moved out of the screen on 50 px
        banner.frame = CGRectOffset(banner.frame, 0, self.view.frame.size.height - bannerView.frame.size.height);

        [UIView commitAnimations];

        bannerIsVisible = YES;

    }
}

- (void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error
{
    NSLog(@"didFailToReceiveAdWithError : %@", [error localizedDescription]);

    if (bannerIsVisible) {

        [UIView beginAnimations:@"animateAdBannerOff" context:NULL];

        // Assumes the banner view is placed at the bottom of the screen.
        banner.frame = CGRectOffset(banner.frame, 0, banner.frame.size.height);

        [UIView commitAnimations];

        bannerIsVisible = NO;

    }

}

Hope you understand above code. Good luck

Cheerio, Mark Thien

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top