智能横幅广告
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
作为一种广告单元,智能横幅可在任何屏幕尺寸、任何屏幕方向的各种设备上展示与屏幕等宽的横幅广告。智能横幅可以检测设备处于当前屏幕方向时的宽度,并据此创建与之等宽的广告视图。
在 iPhone 上,当屏幕为纵向时,智能横幅广告高度为 50 点;当屏幕为横向时,广告高度为 32 点。在 iPad 上,智能横幅广告高度在屏幕为纵向和横向时均为 90 点。
当图片广告不足以占据所分配的整个空间时,系统会居中放置图片,然后填充两侧剩余的空间。

要使用智能横幅广告,只需为广告尺寸指定 kGADAdSizeSmartBannerPortrait
(适用于纵向屏幕)或 kGADAdSizeSmartBannerLandscape
(适用于横向屏幕)即可:
Swift
let bannerView = GADBannerView(adSize: kGADAdSizeSmartBannerPortrait)
Objective-C
GADBannerView *bannerView = [[GADBannerView alloc]
initWithAdSize:kGADAdSizeSmartBannerPortrait];
由于 iOS 11 增加了安全区域,因此对于全宽横幅广告,您还应添加对其边缘的限制,使之不超过安全区域的边缘。以下代码段展示了如何执行此操作:
Swift
func addBannerViewToView(_ bannerView: GADBannerView) {
bannerView.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(bannerView)
if #available(iOS 11.0, *) {
// In iOS 11, we need to constrain the view to the safe area.
positionBannerViewFullWidthAtBottomOfSafeArea(bannerView)
}
else {
// In lower iOS versions, safe area is not available so we use
// bottom layout guide and view edges.
positionBannerViewFullWidthAtBottomOfView(bannerView)
}
}
// MARK: - view positioning
@available (iOS 11, *)
func positionBannerViewFullWidthAtBottomOfSafeArea(_ bannerView: UIView) {
// Position the banner. Stick it to the bottom of the Safe Area.
// Make it constrained to the edges of the safe area.
let guide = view.safeAreaLayoutGuide
NSLayoutConstraint.activate([
guide.leftAnchor.constraint(equalTo: bannerView.leftAnchor),
guide.rightAnchor.constraint(equalTo: bannerView.rightAnchor),
guide.bottomAnchor.constraint(equalTo: bannerView.bottomAnchor)
])
}
func positionBannerViewFullWidthAtBottomOfView(_ bannerView: UIView) {
view.addConstraint(NSLayoutConstraint(item: bannerView,
attribute: .leading,
relatedBy: .equal,
toItem: view,
attribute: .leading,
multiplier: 1,
constant: 0))
view.addConstraint(NSLayoutConstraint(item: bannerView,
attribute: .trailing,
relatedBy: .equal,
toItem: view,
attribute: .trailing,
multiplier: 1,
constant: 0))
view.addConstraint(NSLayoutConstraint(item: bannerView,
attribute: .bottom,
relatedBy: .equal,
toItem: bottomLayoutGuide,
attribute: .top,
multiplier: 1,
constant: 0))
}
Objective-C
- (void)addBannerViewToView:(UIView *)bannerView {
bannerView.translatesAutoresizingMaskIntoConstraints = NO;
[self.view addSubview:bannerView];
if (@available(ios 11.0, *)) {
// In iOS 11, we need to constrain the view to the safe area.
[self positionBannerViewFullWidthAtBottomOfSafeArea:bannerView];
} else {
// In lower iOS versions, safe area is not available so we use
// bottom layout guide and view edges.
[self positionBannerViewFullWidthAtBottomOfView:bannerView];
}
}
#pragma mark - view positioning
- (void)positionBannerViewFullWidthAtBottomOfSafeArea:(UIView *_Nonnull)bannerView NS_AVAILABLE_IOS(11.0) {
// Position the banner. Stick it to the bottom of the Safe Area.
// Make it constrained to the edges of the safe area.
UILayoutGuide *guide = self.view.safeAreaLayoutGuide;
[NSLayoutConstraint activateConstraints:@[
[guide.leftAnchor constraintEqualToAnchor:bannerView.leftAnchor],
[guide.rightAnchor constraintEqualToAnchor:bannerView.rightAnchor],
[guide.bottomAnchor constraintEqualToAnchor:bannerView.bottomAnchor]
]];
}
- (void)positionBannerViewFullWidthAtBottomOfView:(UIView *_Nonnull)bannerView {
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:bannerView
attribute:NSLayoutAttributeLeading
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeLeading
multiplier:1
constant:0]];
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:bannerView
attribute:NSLayoutAttributeTrailing
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeTrailing
multiplier:1
constant:0]];
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:bannerView
attribute:NSLayoutAttributeBottom
relatedBy:NSLayoutRelationEqual
toItem:self.bottomLayoutGuide
attribute:NSLayoutAttributeTop
multiplier:1
constant:0]];
}
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-08-31。
[null,null,["最后更新时间 (UTC):2025-08-31。"],[[["\u003cp\u003eSmart Banners are responsive ad units that adjust their size to fit the width of the device's screen, spanning the full width in either portrait or landscape orientation.\u003c/p\u003e\n"],["\u003cp\u003eOn iPhones, Smart Banners have a height of 50 points in portrait and 32 points in landscape; on iPads, the height is consistently 90 points in both orientations.\u003c/p\u003e\n"],["\u003cp\u003eIt is recommended to use the newer adaptive banners instead of Smart Banners for better ad performance and user experience.\u003c/p\u003e\n"],["\u003cp\u003eFor full-width banners, especially with iOS 11 and later, constraints should be added to align the banner edges with the safe area edges for optimal display.\u003c/p\u003e\n"]]],[],null,["# Smart Banners\n\nSelect platform: [Android](/admob/android/banner/smart \"View this page for the Android platform docs.\") [iOS](/admob/ios/banner/smart \"View this page for the iOS platform docs.\") [Unity](/admob/unity/banner/smart \"View this page for the Unity platform docs.\")\n\n\u003cbr /\u003e\n\n| Try the newer [adaptive banners](/admob/ios/banner/anchored-adaptive) instead.\n\nSmart Banners are ad units that render screen-width banner ads on any screen\nsize across different devices in either orientation. Smart Banners detect the\nwidth of the device in its current orientation and create the ad view that size.\n\nSmart Banners on iPhones have a height of 50 points in portrait and 32 points in\nlandscape. On iPads, height is 90 points in both portrait and landscape.\n\nWhen an image ad isn't large enough to take up the entire allotted space, the\nimage will be centered, and the space on either side will be filled in.\n\nTo use Smart Banners, just specify `kGADAdSizeSmartBannerPortrait`\n(for portait orientation) or `kGADAdSizeSmartBannerLandscape` (for landscape\norientation) for the ad size: \n\n### Swift\n\n```swift\nlet bannerView = GADBannerView(adSize: kGADAdSizeSmartBannerPortrait)\n```\n\n### Objective-C\n\n```objective-c\nGADBannerView *bannerView = [[GADBannerView alloc]\n initWithAdSize:kGADAdSizeSmartBannerPortrait];\n```\n\nSince the addition of the safe area for iOS 11, for full-width banners you\nshould also add constraints for the edges of the banner to the edges of the\nsafe area. Here is a code snippet showing how to do this: \n\n### Swift\n\n```swift\nfunc addBannerViewToView(_ bannerView: GADBannerView) {\n bannerView.translatesAutoresizingMaskIntoConstraints = false\n view.addSubview(bannerView)\n if #available(iOS 11.0, *) {\n // In iOS 11, we need to constrain the view to the safe area.\n positionBannerViewFullWidthAtBottomOfSafeArea(bannerView)\n }\n else {\n // In lower iOS versions, safe area is not available so we use\n // bottom layout guide and view edges.\n positionBannerViewFullWidthAtBottomOfView(bannerView)\n }\n}\n\n// MARK: - view positioning\n@available (iOS 11, *)\nfunc positionBannerViewFullWidthAtBottomOfSafeArea(_ bannerView: UIView) {\n // Position the banner. Stick it to the bottom of the Safe Area.\n // Make it constrained to the edges of the safe area.\n let guide = view.safeAreaLayoutGuide\n NSLayoutConstraint.activate([\n guide.leftAnchor.constraint(equalTo: bannerView.leftAnchor),\n guide.rightAnchor.constraint(equalTo: bannerView.rightAnchor),\n guide.bottomAnchor.constraint(equalTo: bannerView.bottomAnchor)\n ])\n}\n\nfunc positionBannerViewFullWidthAtBottomOfView(_ bannerView: UIView) {\n view.addConstraint(NSLayoutConstraint(item: bannerView,\n attribute: .leading,\n relatedBy: .equal,\n toItem: view,\n attribute: .leading,\n multiplier: 1,\n constant: 0))\n view.addConstraint(NSLayoutConstraint(item: bannerView,\n attribute: .trailing,\n relatedBy: .equal,\n toItem: view,\n attribute: .trailing,\n multiplier: 1,\n constant: 0))\n view.addConstraint(NSLayoutConstraint(item: bannerView,\n attribute: .bottom,\n relatedBy: .equal,\n toItem: bottomLayoutGuide,\n attribute: .top,\n multiplier: 1,\n constant: 0))\n}\n```\n\n### Objective-C\n\n```objective-c\n- (void)addBannerViewToView:(UIView *)bannerView {\n bannerView.translatesAutoresizingMaskIntoConstraints = NO;\n [self.view addSubview:bannerView];\n if (@available(ios 11.0, *)) {\n // In iOS 11, we need to constrain the view to the safe area.\n [self positionBannerViewFullWidthAtBottomOfSafeArea:bannerView];\n } else {\n // In lower iOS versions, safe area is not available so we use\n // bottom layout guide and view edges.\n [self positionBannerViewFullWidthAtBottomOfView:bannerView];\n }\n}\n\n#pragma mark - view positioning\n\n- (void)positionBannerViewFullWidthAtBottomOfSafeArea:(UIView *_Nonnull)bannerView NS_AVAILABLE_IOS(11.0) {\n // Position the banner. Stick it to the bottom of the Safe Area.\n // Make it constrained to the edges of the safe area.\n UILayoutGuide *guide = self.view.safeAreaLayoutGuide;\n\n [NSLayoutConstraint activateConstraints:@[\n [guide.leftAnchor constraintEqualToAnchor:bannerView.leftAnchor],\n [guide.rightAnchor constraintEqualToAnchor:bannerView.rightAnchor],\n [guide.bottomAnchor constraintEqualToAnchor:bannerView.bottomAnchor]\n ]];\n}\n\n- (void)positionBannerViewFullWidthAtBottomOfView:(UIView *_Nonnull)bannerView {\n [self.view addConstraint:[NSLayoutConstraint constraintWithItem:bannerView\n attribute:NSLayoutAttributeLeading\n relatedBy:NSLayoutRelationEqual\n toItem:self.view\n attribute:NSLayoutAttributeLeading\n multiplier:1\n constant:0]];\n [self.view addConstraint:[NSLayoutConstraint constraintWithItem:bannerView\n attribute:NSLayoutAttributeTrailing\n relatedBy:NSLayoutRelationEqual\n toItem:self.view\n attribute:NSLayoutAttributeTrailing\n multiplier:1\n constant:0]];\n [self.view addConstraint:[NSLayoutConstraint constraintWithItem:bannerView\n attribute:NSLayoutAttributeBottom\n relatedBy:NSLayoutRelationEqual\n toItem:self.bottomLayoutGuide\n attribute:NSLayoutAttributeTop\n multiplier:1\n constant:0]];\n}\n```"]]