Native Styles

Native style settings enable Google Ad Manager to handle the rendering of your native ads based on native styles you specify within the product. First, specify size and targeting. Then add HTML, CSS, and JavaScript to define ads that are responsive and produce a quality display across all screens. You don't need to do any of the rendering; Ad Manager automatically applies the right native style for the destination. Native styles are implemented just like banner ads, using a GAMBannerView. They can be used with a fixed ad size determined ahead of time, or a fluid ad size determined at runtime.

Prerequisites

  • Google Mobile Ads SDK version 7.14.0 or higher

This guide assumes some working knowledge of the Google Mobile Ads SDK. If you haven't already done so, consider running through our Get Started guide.

Fixed size

Native styles with a fixed size allow you to control the width and height of the native ad. To set a fixed size, follow these steps:

  1. Create a line item in the Ad Manager UI and select one of the predefined sizes from the Size field dropdown.

  2. In Interface Builder, set the width and height of the GAMBannerView to match the predefined size you selected in step 1. You can see a list of sizes and their corresponding GADAdSize constants in the Banner size section.

Implementing native styles with a fixed size is as simple as following the instructions in Your first banner request, but you gain the flexibility and control over the HTML, CSS, and JavaScript to give your banner ad a native look and feel that's natural in your app.

Fluid size

In some cases, a fixed size may not make sense. For example, you may want the width of the ad to match your app's content, but need its height to dynamically adjust to fit the ad's content. To handle this case, you can specify Fluid as the ad size in the Ad Manager UI, which designates that the size of the ad is determined at runtime in the app. The SDK provides a special GADAdSize constant, kGADAdSizeFluid, to handle this case. The fluid ad size height is dynamically determined based on the publisher defined width, allowing the GAMBannerView, to adjust its height to match that of the creative.

Fluid request

Unlike other ad formats, the kGADAdSizeFluid ad size does not have a predefined width, so make sure to explicitly set the banner's frame width in your code or in Interface Builder. If a width is not specified, the SDK by default sets the height of the banner based on the full width of the device.

If you make a multisize request that includes kGADAdSizeFluid, the ad returned is always placed inside a fluid container and behave like a fluid ad. In the event that a non-fluid creative is returned in this fluid container, the SDK centers the ad in the container so that you don't have to modify the width constraint each time a new ad is returned.

The implementation for making a single size and a multisize fluid request is very similar; the only difference is for a multisize request, you set the validAdSizes property to specify the ad sizes that are valid for the ad request:

Swift

bannerView.validAdSizes = [NSValueFromGADAdSize(kGADAdSizeFluid),
                           NSValueFromGADAdSize(kGADAdSizeBanner)]

Objective-C

_bannerView.validAdSizes = @[ NSValueFromGADAdSize(kGADAdSizeFluid),
                              NSValueFromGADAdSize(kGADAdSizeBanner) ];

Here's what the full implementation looks like in code:

Swift

var bannerView: GAMBannerView!

override func viewDidLoad() {
  super.viewDidLoad()
  // Create the GAMBannerView and set its width to a width that makes sense for your
  // app. In this example, the width is set to the width of the UIViewController's
  // root view.
  bannerView = GAMBannerView(adSize: kGADAdSizeFluid)
  var frameRect = bannerView.frame
  frameRect.size.width = view.bounds.width
  bannerView.frame = frameRect

  // Uncomment this code for a multisize fluid request.
  // bannerView.validAdSizes = [NSValueFromGADAdSize(kGADAdSizeFluid),
  //                            NSValueFromGADAdSize(kGADAdSizeBanner)]

  bannerView.adUnitID = "YOUR_AD_UNIT_ID"
  bannerView.rootViewController = self

  // Make the ad request.
  bannerView.load(GAMRequest())
}

Objective-C

GAMBannerView *_bannerView;

- (void)viewDidLoad {
  [super viewDidLoad];
  // Create the GAMBannerView and set its width to a width that makes sense for your
  // app. In this example, the width is set to the width of the UIViewController's
  // root view.
  _bannerView = [[GAMBannerView alloc] initWithAdSize:kGADAdSizeFluid];
  CGRect frameRect = _bannerView.frame;
  frameRect.size.width = CGRectGetWidth(self.view.bounds);
  _bannerView.frame = frameRect;

  // Uncomment this code for a multisize fluid request.
  // _bannerView.validAdSizes = @[ NSValueFromGADAdSize(kGADAdSizeFluid),
  //                               NSValueFromGADAdSize(kGADAdSizeBanner) ];

  _bannerView.adUnitID = @"YOUR_AD_UNIT_ID";
  _bannerView.rootViewController = self;

  // Make the ad request.
  [_bannerView loadRequest:[GAMRequest request]];
}

To see an example implementation of the Ad Manager Fluid ad size, download the iOS API Demo app in Swift or Objective-C.

Download API Demo

GADAdSizeDelegate protocol

You may want to know the height of a banner before its ad size changes. The adView:willChangeAdSizeTo: callback notifies its delegate before the banner view changes to the new GADAdSize. To be notified before the banner view changes to the new ad size, your class must conform to the GADAdSizeDelegate protocol.

Here's a sample implementation of the adView:willChangeAdSizeTo: callback that shows how to get the new width and height of the banner:

Swift

// To be notified before the banner's ad size changes, your view controller class must
// conform to the GADAdSizeDelegate protocol.
bannerView.adSizeDelegate = self

// MARK: - GADAdSizeDelegate

func adView(_ bannerView: GADBannerView, willChangeAdSizeTo adSize: GADAdSize) {
  let height = adSize.size.height
  let width = adSize.size.width
}

Objective-C

// To be notified before the banner's ad size changes, your view controller class must
// conform to the GADAdSizeDelegate protocol.
_bannerView.adSizeDelegate = self;

#pragma mark - GADAdSizeDelegate

- (void)adView:(GADBannerView *)bannerView willChangeAdSizeTo:(GADAdSize)adSize {
  CGFloat height = adSize.size.height;
  CGFloat width = adSize.size.width;
}