3

Does anybody know how to show 50pt-height GADBannerView programmatically?

In my viewDidLoad,

int heightOfBanner = 50;
GADBannerView *banner = 
 [[GADBannerView alloc]
 initWithAdSize:GADAdSizeFullWidthPortraitWithHeight(heightOfBanner)
 origin:CGPointMake(0, CGSizeFromGADAdSize(kGADAdSizeBanner).height)];
banner.adUnitID = @"ca-app-pub-xxxxxxxxxxxxxxxxxxxxxxxxxxx";
banner.rootViewController = self;
[self.view addSubview:banner];

[banner loadRequest:[GADRequest request]];

Above code does't show anything.

But when heightOfBanner set to be higher value(for example, 200), it works well(but banner's height is not 50).

My podfile is below:

source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '8.0'
pod 'Google-Mobile-Ads-SDK', '~> 7.0'

and I did "pod install" successfully.

nhgrif
  • 61,578
  • 25
  • 134
  • 173
Tsuyoshi Endo
  • 727
  • 1
  • 7
  • 17

1 Answers1

2

Don't use exact dimensions to draw your GADBannerView. You should set its frame and origin relative to the screen dimensions. This will display your GADBannerView on the bottom of the screen

    // Get device screen size
    // For example, screenBounds on an iPhone 6 will look like this
    // screenBounds.origin.x == 0
    // screenBounds.origin.y == 0
    // screenBounds.size.width == 375
    // screenBounds.size.height == 667
    CGRect screenBounds = [[UIScreen mainScreen] bounds];

    // Setup AdMob view
    // Create the GADBannerView
    adMobView = [[GADBannerView alloc] initWithAdSize:kGADAdSizeBanner];

    // Use your BANNER_UNIT_ID
    adMobView.adUnitID = BANNER_UNIT_ID;
    adMobView.rootViewController = self;
    [adMobView loadRequest:[GADRequest request]];

    // This sets the frame origin at (0,0) which would be the top left of the device screen
    // screenBounds.size.width and adMobView.bounds.size.height sets the size of the GADBannerView
    [adMobView setFrame:CGRectMake(0, 0, screenBounds.size.width, adMobView.bounds.size.height)];

    // This will take the center of our GADBannerView and move it to a point (x,y)
    // We want our GADBannerView.center in the center of the device screen
    // So lets get the width of our screen and divide it by 2. We do this with screenBounds.size.width / 2
    // We also want our GADBannerView to be at the bottom of the screen
    // So lets get the height of our screen with screenBounds.size.height
    // Remember were talking about the center of our GADBannerView here so if we just set it to that
    // Half of our GADBannerView's height will be cut off by the bottom of the screen
    // So lets subtract half of our GADBannerView's height to fix that with adMobView.bounds.size.height / 2
    adMobView.center = CGPointMake(screenBounds.size.width / 2, screenBounds.size.height - (adMobView.bounds.size.height / 2));

    // Add it to our view
    [self.view addSubview:adMobView];
Daniel Storm
  • 18,301
  • 9
  • 84
  • 152
  • @TsuyoshiEndo I've updated my answer so the width of the `GADBannerView` stretches across the width of entire screen. – Daniel Storm May 02 '15 at 14:07
  • It works well!! I understood _Don't use exact dimensions_ and _use center_ when I want to locate specific point. So, When I use one of UIViewControllers in UINavigationController, does this code work well? – Tsuyoshi Endo May 02 '15 at 14:25
  • I got it. [I made it temporarily (so dirty..) but should work also in UINavigationController](https://github.com/etsuyoshi/UINavigationWithGADBannerView.git) using @DanielStorm code. Thank you for a good answer. – Tsuyoshi Endo May 02 '15 at 16:23