固定大小橫幅廣告

當自動調整橫幅廣告不符合需求時,Google Mobile Ads SDK 支援固定廣告大小。

下表列出標準橫幅廣告大小。

大小 (以 dp 為單位,寬 x 高) 說明 適用國家/地區 AdSize 常數
320 x 50 橫幅廣告 手機和平板電腦 GADAdSizeBanner
320x100 大型橫幅 手機和平板電腦 GADAdSizeLargeBanner
300 x 250 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 UI 中,建立指定與不同大小廣告素材關聯的相同廣告單元的委刊項。

  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