varbannerView:AdManagerBannerView!overridefuncviewDidLoad(){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=AdManagerBannerView(adSize:AdSizeFluid)varframeRect=bannerView.frameframeRect.size.width=view.bounds.widthbannerView.frame=frameRect// Uncomment this code for a multisize fluid request.// bannerView.validAdSizes = [nsValue(for: AdSizeFluid), nsValue(for: AdSizeBanner)]bannerView.adUnitID="YOUR_AD_UNIT_ID"bannerView.rootViewController=self// Make the ad request.bannerView.load(AdManagerRequest())}
Objective-C
GAMBannerView*_bannerView;-(void)viewDidLoad{[superviewDidLoad];// 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=[[GAMBannerViewalloc]initWithAdSize:kGADAdSizeFluid];CGRectframeRect=_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.[_bannerViewloadRequest:[GAMRequestrequest]];}
如需查看 Ad Manager 流式广告尺寸的实现示例,请下载 Swift 或 Objective-C 语言的 iOS API Demo 应用。
// To be notified before the banner's ad size changes, your view controller class must// conform to the GADAdSizeDelegate protocol.bannerView.adSizeDelegate=self// MARK: - GADAdSizeDelegatefuncadView(_bannerView:BannerView,willChangeAdSizeToadSize:AdSize){letheight=adSize.size.heightletwidth=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;
#pragmamark-GADAdSizeDelegate-(void)adView:(GADBannerView*)bannerViewwillChangeAdSizeTo:(GADAdSize)adSize{CGFloatheight=adSize.size.height;CGFloatwidth=adSize.size.width;}
[null,null,["最后更新时间 (UTC):2025-08-27。"],[[["\u003cp\u003eNative Styles in Google Ad Manager allow you to customize the look and feel of native ads using HTML, CSS, and JavaScript, rendering them seamlessly within your app.\u003c/p\u003e\n"],["\u003cp\u003eYou can define fixed-size native ads by specifying their dimensions in Ad Manager and your app's layout, similar to implementing banner ads.\u003c/p\u003e\n"],["\u003cp\u003eFluid-size native ads adjust their height dynamically based on content and available width, requiring you to set the desired width in your app's code.\u003c/p\u003e\n"],["\u003cp\u003eTo get notified before a banner's size changes (especially useful for fluid ads), implement the \u003ccode\u003eGADAdSizeDelegate\u003c/code\u003e protocol and its \u003ccode\u003eadView:willChangeAdSizeTo:\u003c/code\u003e method.\u003c/p\u003e\n"],["\u003cp\u003eYou can download the iOS API Demo app for example implementations of Native Styles, including fluid ad sizes.\u003c/p\u003e\n"]]],["Native styles in Google Ad Manager utilize HTML, CSS, and JavaScript for ad rendering. Ad size can be fixed by setting predefined dimensions in the UI and `GAMBannerView`, or fluid, adjusting dynamically at runtime with `kGADAdSizeFluid`. Fluid ads require setting the banner's frame width. Multisize requests can include fluid and other ad sizes. `GADAdSizeDelegate` enables notification before banner size changes, providing access to the new width and height.\n"],null,["# Native Styles\n\nSelect platform: [Android](/ad-manager/mobile-ads-sdk/android/native/styles \"View this page for the Android platform docs.\") [iOS](/ad-manager/mobile-ads-sdk/ios/native/styles \"View this page for the iOS platform docs.\") [Flutter](/ad-manager/mobile-ads-sdk/flutter/native/styles \"View this page for the Flutter platform docs.\")\n\n\u003cbr /\u003e\n\n| **Note:** Native Styles use HTML and CSS that you configure in Google Ad Manager. If you're looking to render your own in-app ads using native objects, please see [Native Advanced](/ad-manager/mobile-ads-sdk/ios/native/advanced).\n\n[Native style settings](//support.google.com/admanager/answer/6366914)\nenable Google Ad Manager to handle the rendering of your native ads based on native\nstyles you specify within the product. First, specify size and targeting.\nThen add HTML, CSS, and JavaScript to define ads that are responsive\nand produce a quality display across all screens. You don't need to do\nany of the rendering; Ad Manager automatically applies the right native style\nfor the destination. Native styles are implemented just like banner ads,\nusing a `GAMBannerView`. They can be used with a fixed ad\nsize determined ahead of time, or a fluid ad size determined at runtime.\n\nPrerequisites\n-------------\n\n- Google Mobile Ads SDK version 7.14.0 or higher\n\nThis guide assumes some working knowledge of the Google Mobile Ads SDK.\nIf you haven't already done so, consider running through our\n[Get Started](/ad-manager/mobile-ads-sdk/ios/quick-start) guide.\n\nFixed size\n----------\n\nNative styles with a fixed size allow you to control the width and height\nof the native ad. To set a fixed size, follow these steps:\n\n1. Create a line item in the Ad Manager UI and select one of the\n predefined sizes from the `Size` field dropdown.\n\n2. In Interface Builder, set the width and height of the `GAMBannerView`\n to match the predefined size you selected in step 1. You can see a\n list of sizes and their corresponding `GADAdSize` constants in the [Banner\n size](/ad-manager/mobile-ads-sdk/ios/banner#banner_size)\n section.\n\nImplementing native styles with a fixed size is as simple as following\nthe instructions in [Your first banner\nrequest](/ad-manager/mobile-ads-sdk/ios/quick-start#your_first_banner_request),\nbut you gain the flexibility and control over the HTML, CSS, and JavaScript\nto give your banner ad a native look and feel that's natural in your app.\n\nFluid size\n----------\n\nIn some cases, a fixed size may not make sense. For example, you may\nwant the width of the ad to match your app's content, but need its height\nto dynamically adjust to fit the ad's content. To handle this case,\nyou can specify `Fluid` as the ad size in the Ad Manager UI, which\ndesignates that the size of the ad is determined at runtime in the app.\nThe SDK provides a special `GADAdSize` constant,\n[`kGADAdSizeFluid`](/ad-manager/mobile-ads-sdk/ios/api/reference/Constants#/c:@kGADAdSizeFluid),\nto handle this case. The fluid ad size height is dynamically determined\nbased on the publisher defined width, allowing the\n[`GAMBannerView`](/ad-manager/mobile-ads-sdk/ios/api/reference/Classes/GAMBannerView),\nto adjust its height to match that of the creative.\n\n### Fluid request\n\nUnlike other ad formats, the `kGADAdSizeFluid` ad size does not have\na predefined width, so make sure to explicitly set the banner's frame\nwidth in your code or in Interface Builder. If a width is not specified,\nthe SDK by default sets the height of the banner based on the full\nwidth of the device.\n\nIf you make a multisize request that includes `kGADAdSizeFluid`,\nthe ad returned is always placed inside a fluid container and\nbehave like a fluid ad. In the event that a non-fluid creative is\nreturned in this fluid container, the SDK centers the ad in the\ncontainer so that you don't have to modify the width constraint\neach time a new ad is returned.\n\nThe implementation for making a single size and a multisize fluid\nrequest is very similar; the only difference is for a multisize\nrequest, you set the `validAdSizes` property to specify the ad\nsizes that are valid for the ad request: \n\n### Swift\n\n bannerView.validAdSizes = [nsValue(for: AdSizeFluid), nsValue(for: AdSizeBanner)]\n\n### Objective-C\n\n _bannerView.validAdSizes = @[ NSValueFromGADAdSize(kGADAdSizeFluid),\n NSValueFromGADAdSize(kGADAdSizeBanner) ];\n\nHere's what the full implementation looks like in code: \n\n### Swift\n\n var bannerView: AdManagerBannerView!\n\n override func viewDidLoad() {\n super.viewDidLoad()\n // Create the GAMBannerView and set its width to a width that makes sense for your\n // app. In this example, the width is set to the width of the UIViewController's\n // root view.\n bannerView = AdManagerBannerView(adSize: AdSizeFluid)\n var frameRect = bannerView.frame\n frameRect.size.width = view.bounds.width\n bannerView.frame = frameRect\n\n // Uncomment this code for a multisize fluid request.\n // bannerView.validAdSizes = [nsValue(for: AdSizeFluid), nsValue(for: AdSizeBanner)]\n\n bannerView.adUnitID = \"YOUR_AD_UNIT_ID\"\n bannerView.rootViewController = self\n\n // Make the ad request.\n bannerView.load(AdManagerRequest())\n }\n\n### Objective-C\n\n GAMBannerView *_bannerView;\n\n - (void)viewDidLoad {\n [super viewDidLoad];\n // Create the GAMBannerView and set its width to a width that makes sense for your\n // app. In this example, the width is set to the width of the UIViewController's\n // root view.\n _bannerView = [[GAMBannerView alloc] initWithAdSize:kGADAdSizeFluid];\n CGRect frameRect = _bannerView.frame;\n frameRect.size.width = CGRectGetWidth(self.view.bounds);\n _bannerView.frame = frameRect;\n\n // Uncomment this code for a multisize fluid request.\n // _bannerView.validAdSizes = @[ NSValueFromGADAdSize(kGADAdSizeFluid),\n // NSValueFromGADAdSize(kGADAdSizeBanner) ];\n\n _bannerView.adUnitID = @\"YOUR_AD_UNIT_ID\";\n _bannerView.rootViewController = self;\n\n // Make the ad request.\n [_bannerView loadRequest:[GAMRequest request]];\n }\n\nTo see an example implementation of the Ad Manager Fluid ad size,\ndownload the iOS API Demo app in Swift or Objective-C.\n\n[Download API Demo](//github.com/googleads/googleads-mobile-ios-examples/releases/latest)\n\nGADAdSizeDelegate protocol\n--------------------------\n\nYou may want to know the height of a banner before its ad size changes.\nThe `adView:willChangeAdSizeTo:` callback notifies its delegate before\nthe banner view changes to the new `GADAdSize`. To be notified before\nthe banner view changes to the new ad size, your class must conform to\nthe `GADAdSizeDelegate` protocol.\n\nHere's a sample implementation of the `adView:willChangeAdSizeTo:`\ncallback that shows how to get the new width and height of the banner: \n\n### Swift\n\n // To be notified before the banner's ad size changes, your view controller class must\n // conform to the GADAdSizeDelegate protocol.\n bannerView.adSizeDelegate = self\n\n // MARK: - GADAdSizeDelegate\n\n func adView(_ bannerView: BannerView, willChangeAdSizeTo adSize: AdSize) {\n let height = adSize.size.height\n let width = adSize.size.width\n }\n\n### Objective-C\n\n // To be notified before the banner's ad size changes, your view controller class must\n // conform to the GADAdSizeDelegate protocol.\n _bannerView.adSizeDelegate = self;\n\n #pragma mark - GADAdSizeDelegate\n\n - (void)adView:(GADBannerView *)bannerView willChangeAdSizeTo:(GADAdSize)adSize {\n CGFloat height = adSize.size.height;\n CGFloat width = adSize.size.width;\n }"]]