固定尺寸横幅广告

Google 移动广告 SDK 支持在自适应横幅广告无法满足您需求的情况下使用固定的广告尺寸。

下表列出了标准的横幅广告尺寸。

尺寸(宽 x 高,以 dp 为单位) 说明 可用性 AdSize 常量
320x50 横幅 手机和平板电脑 GADAdSizeBanner
320x100 大型横幅 手机和平板电脑 GADAdSizeLargeBanner
300x250 IAB 中矩形广告 手机和平板电脑 GADAdSizeMediumRectangle
468x60 IAB 全尺寸横幅 平板电脑 GADAdSizeFullBanner
728x90 IAB 页首横幅 平板电脑 GADAdSizeLeaderboard

要自定义横幅广告尺寸,请使用 GADAdSizeFromCGSize 设置尺寸:

Swift

let adSize = GADAdSizeFromCGSize(CGSize(width: 250, height: 250))

Objective-C

GADAdSize size = GADAdSizeFromCGSize(CGSizeMake(250, 250));

固定尺寸横幅广告示例

Swift Objective-C

自定义广告尺寸

除了标准广告单元之外,Google Ad Manager 还允许您在应用中投放任何尺寸的广告单元。为广告请求定义的广告尺寸(宽度、高度)应与应用中所显示广告视图 (GAMBannerView) 的尺寸相匹配。要设置自定义尺寸,请使用 GADAdSizeFromCGSize

Swift

// Define custom GADAdSize of 250x250 for GAMBannerView.
let customAdSize = GADAdSizeFromCGSize(CGSize(width: 250, height: 250))
bannerView = GAMBannerView(adSize: customAdSize)

Objective-C

// Define custom GADAdSize of 250x250 for GAMBannerView
GADAdSize customAdSize = GADAdSizeFromCGSize(CGSizeMake(250, 250));
self.bannerView = [[GAMBannerView alloc] initWithAdSize:customAdSize];

多种广告尺寸

Ad Manager 允许您指定多个可能适合投放到 GAMBannerView 的广告尺寸。使用此功能需要执行以下三个步骤:

  1. 在 Ad Manager 界面中,创建一个订单项,定位到关联了不同尺寸广告素材的同一广告单元。

  2. 在您应用中的 GAMBannerView 上设置 validAdSizes 属性:

    Swift

    // Define an optional array of GADAdSize to specify all valid sizes that are appropriate
    // for this slot. Never create your own GADAdSize directly. Use one of the
    // predefined standard ad sizes (such as GADAdSizeBanner), or create one using
    // the GADAdSizeFromCGSize method.
    //
    // Note: Ensure that the allocated GAMBannerView is defined with an ad size. Also note
    // that all desired sizes should be included in the validAdSizes array.
    bannerView.validAdSizes = [NSValueFromGADAdSize(GADAdSizeBanner),
        NSValueFromGADAdSize(GADAdSizeMediumRectangle),
        NSValueFromGADAdSize(GADAdSizeFromCGSize(CGSize(width: 120, height: 20)))]
    

    Objective-C

    // Define an optional array of GADAdSize to specify all valid sizes that are appropriate
    // for this slot. Never create your own GADAdSize directly. Use one of the
    // predefined standard ad sizes (such as GADAdSizeBanner), or create one using
    // the GADAdSizeFromCGSize method.
    //
    // Note: Ensure that the allocated GAMBannerView is defined with an ad size. Also note
    // that all desired sizes should be included in the validAdSizes array.
    self.bannerView.validAdSizes = @[
        NSValueFromGADAdSize(GADAdSizeBanner),
        NSValueFromGADAdSize(GADAdSizeMediumRectangle),
        NSValueFromGADAdSize(GADAdSizeFromCGSize(CGSizeMake(120, 20)))
    ];
    
  3. 实现 GADAdSizeDelegate 方法以检测广告尺寸更改。

    Swift

    public func bannerView(_ bannerView: GADBannerView, willChangeAdSizeTo size: GADAdSize)
    

    Objective-C

    - (void)bannerView:(GAMBannerView *)view willChangeAdSizeTo:(GADAdSize)size;
    

    在发出广告请求前,请务必先设置委托。

    Swift

    bannerView.adSizeDelegate = self
    

    Objective-C

    self.bannerView.adSizeDelegate = self;
    

多种广告尺寸示例

Swift Objective-C