Skip to content

Instantly share code, notes, and snippets.

@jrcryer
Created October 7, 2012 19:37
Show Gist options
  • Save jrcryer/3849335 to your computer and use it in GitHub Desktop.
Save jrcryer/3849335 to your computer and use it in GitHub Desktop.
Obj-C Category for adding iAds to UIViewController
//
// 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
//
// 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