Created
October 7, 2012 19:37
-
-
Save jrcryer/3849335 to your computer and use it in GitHub Desktop.
Obj-C Category for adding iAds to UIViewController
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// UIViewController+iAd.h | |
// | |
// Created by James Cryer on 04/10/2012. | |
// | |
// | |
#import <UIKit/UIKit.h> | |
#import "iAd/iAd.h" | |
@interface UIViewController (iAd) | |
- (int)getBannerHeight:(UIDeviceOrientation)orientation; | |
- (int)getBannerHeight; | |
- (ADBannerView *)bannerViewWithDelegate:(id)delegate; | |
- (void)animateContentForAd:(ADBannerView *)adBannerView contentView:(UIView *)contentView atOrientation:(UIDeviceOrientation)orientation andVisible:(BOOL)visible; | |
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// UIViewController+iAd.m | |
// | |
// Created by James Cryer on 04/10/2012. | |
// | |
// | |
#import "UIViewController+iAd.h" | |
@implementation UIViewController (iAd) | |
- (int)getBannerHeight:(UIDeviceOrientation)orientation { | |
if (UIInterfaceOrientationIsLandscape(orientation)) { | |
return 32; | |
} else { | |
return 50; | |
} | |
} | |
- (int)getBannerHeight { | |
return [self getBannerHeight:[UIDevice currentDevice].orientation]; | |
} | |
- (ADBannerView *)bannerViewWithDelegate:(id)delegate { | |
ADBannerView *adBannerView = [[ADBannerView alloc] initWithFrame:CGRectZero]; | |
[adBannerView setRequiredContentSizeIdentifiers:[NSSet setWithObjects: | |
ADBannerContentSizeIdentifierPortrait, | |
ADBannerContentSizeIdentifierLandscape, nil]]; | |
if (UIInterfaceOrientationIsLandscape([UIDevice currentDevice].orientation)) { | |
[adBannerView setCurrentContentSizeIdentifier:ADBannerContentSizeIdentifierLandscape]; | |
} else { | |
[adBannerView setCurrentContentSizeIdentifier:ADBannerContentSizeIdentifierPortrait]; | |
} | |
[adBannerView setFrame:CGRectOffset([adBannerView frame], 0, self.view.frame.size.height + [self getBannerHeight])]; | |
[adBannerView setBackgroundColor:[UIColor clearColor]]; | |
[adBannerView setDelegate:delegate]; | |
[self.view addSubview:adBannerView]; | |
return adBannerView; | |
} | |
- (void)animateContentForAd:(ADBannerView *)adBannerView contentView:(UIView *)contentView atOrientation:(UIDeviceOrientation)orientation andVisible:(BOOL)visible | |
{ | |
if (UIInterfaceOrientationIsLandscape(orientation)) { | |
[adBannerView setCurrentContentSizeIdentifier:ADBannerContentSizeIdentifierLandscape]; | |
} else { | |
[adBannerView setCurrentContentSizeIdentifier:ADBannerContentSizeIdentifierPortrait]; | |
} | |
CGRect superViewFrame = self.view.frame; | |
CGRect contentViewFrame = contentView.frame; | |
CGRect adBannerViewFrame = [adBannerView frame]; | |
[UIView beginAnimations:@"animateView" context:nil]; | |
if (visible) { | |
adBannerViewFrame.origin.y = superViewFrame.size.height - [self getBannerHeight:orientation]; | |
[adBannerView setFrame:adBannerViewFrame]; | |
contentViewFrame.size.height = superViewFrame.size.height - [self getBannerHeight:orientation]; | |
contentView.frame = contentViewFrame; | |
} else { | |
adBannerViewFrame.origin.y = -[self getBannerHeight:orientation]; | |
[contentView setFrame:adBannerViewFrame]; | |
contentViewFrame.origin.y = 0; | |
contentViewFrame.size.height = superViewFrame.size.height; | |
contentView.frame = contentViewFrame; | |
} | |
[UIView commitAnimations]; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment