Leon
Leon

Reputation: 3726

iAd covers my view on the first load

I have iAds working almost perfectly using a modified version of UITabBarController-iAds. The only issue I have is on the very first load of an advert my view gets covered. Once the ad refreshes after 30 seconds the view resizes just fine.

I'm using the following code:

- (void)layoutBanner 
{ 
    float height = 0.0; 
    ADBannerView *_banner = (ADBannerView *)[self.view viewWithTag:12]; 
    CGRect bounds = [UIScreen mainScreen].bounds; 

    if (UIInterfaceOrientationIsPortrait(self.interfaceOrientation)) { 
        _banner.currentContentSizeIdentifier = ADBannerContentSizeIdentifierPortrait; 
    } else { 
        _banner.currentContentSizeIdentifier = ADBannerContentSizeIdentifierLandscape; 
    } 

    //When compiling against iOS 8 SDK this needs to be height, regardless of the actual device orientation 
    height = bounds.size.height; 

    CGSize bannerSize = [ADBannerView sizeFromBannerContentSizeIdentifier:_banner.currentContentSizeIdentifier]; 

    //Get content view 
    UIView *_contentView = self.selectedViewController.view; 

    CGRect contentFrame = _contentView.frame; 
    CGRect bannerFrame = _banner.frame; 

    if (_banner.isBannerLoaded) { 
        if (iOS7) { 
            contentFrame.size.height = height - bannerSize.height; 
            bannerFrame.origin.y = contentFrame.size.height - self.tabBar.frame.size.height; 
        } else { 
            contentFrame.size.height = height - self.tabBar.frame.size.height - bannerSize.height; 
            bannerFrame.origin.y = contentFrame.size.height; 
        } 
    } else { 
        if (iOS7) { 
            contentFrame.size.height = height; 
            bannerFrame.origin.y = bounds.size.height; 
        } else { 
            contentFrame.size.height = height - self.tabBar.frame.size.height; 
            bannerFrame.origin.y = bounds.size.height; 
        } 
    } 

    [UIView animateWithDuration:0.25 animations:^{ 
        _banner.frame = bannerFrame; 
        _contentView.frame = contentFrame; 
    } completion:^(BOOL finished) { 
        [self.view bringSubviewToFront:_banner]; 
    }]; 
}

- (void)bannerViewDidLoadAd:(ADBannerView *)banner 
{ 
    NSLog(@"Did load"); 
    [self layoutBanner]; 
} 

Any ideas?

Upvotes: 1

Views: 172

Answers (3)

Leon
Leon

Reputation: 3726

I managed to fix this issue by playing around with the animation block. I moved the content view line and it resolved the issue

 [UIView animateWithDuration:0.25 animations:^{
 _banner.frame = bannerFrame;
 } completion:^(BOOL finished) {
     _contentView.frame = contentFrame;
     [self.view bringSubviewToFront:_banner];
 }];

Upvotes: 2

Jef
Jef

Reputation: 4728

  1. as of iOs8 [[UIScreen mainScreen]bounds] has changed.. previously it would always return portrait (where H > W ) values, regardless of interfaceOrientation, but it now appears to respect interface orientation... (which is not a bad thing, unless you're taking previous behaviour for granted)

  2. Why don't you try CGRectGetMinY(CGRect aRect) ..

eg. (Im assuming from your code the banner goes along the screen bottom, and your TAbBar across the top. (which I think is weird..) )

    //once you have calculated bannerFrame

    contentFrame.size.height = ( CGRectGetMinY(bannerFrame)  - CGRectGetMinY (contentView.frame)  );

contentFrame.origin.y = CGRectGetMaxY(self.tabbar.frame);
  1. Do you have any constraints / resizingMasks set? when you load a second ad the view controller's view has already been resized to fit an ad, presumably the first ad is the one trying to change things..

Upvotes: 0

Artur Kucaj
Artur Kucaj

Reputation: 1081

I'm not 100% sure, but I suppose the problem is related to:

[self.view bringSubviewToFront:_banner];

The whole animation:

[UIView animateWithDuration:0.25 animations:^{ 
        _banner.frame = bannerFrame; 
        _contentView.frame = contentFrame; 
    } completion:^(BOOL finished) { 
        [self.view bringSubviewToFront:_banner]; 
    }];

is a little strange, because at the completion block you are bringing the banner view to front, so I can suppose that before animation did start - the banner was covered by some other view or views - what is the reason of frame animation if banner view is covered - thus cannot be seen while animation is in progress?

Of course the issue applies only for the first run.

Upvotes: 0

Related Questions