2015-05-02 120 views
2

有没有人知道如何以编程方式显示50pt高度的GADBannerView?GADBannerView没有设置高度

在我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]]; 

以上代码简化版,显示任何东西。

但是当heightOfBanner设置为较高值(例如200)时,它运行良好(但横幅的高度不是50)。

我podfile低于:

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

,我也 “吊舱安装” 成功。

回答

1

请勿使用精确的尺寸绘制GADBannerView。您应该相对于屏幕尺寸设置其框架和原点。这将在屏幕底部显示您的GADBannerView

// 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]; 
+0

它不起作用。但它似乎是正确的。 –

+0

@TsuyoshiEndo我已经更新了我的答案,因此'GADBannerView'的宽度横跨整个屏幕的宽度。 –

+0

它运作良好! 我了解_当我想要定位特定的点时,请不要使用精确的dimensions_和_use center_。 所以,当我在UINavigationController中使用一个UIViewControllers时,这个代码是否工作正常? –