自适应横幅广告是新一代自适应广告,可针对每台设备优化广告尺寸,从而最大限度地提升广告效果。自适应横幅广告基于仅支持固定高度的智能横幅广告进行了完善,可让开发者指定广告宽度,进而据此确定最佳广告尺寸。
为了选择最佳广告尺寸,自适应横幅广告采用的是固定宽高比,而非固定高度。因此,这种横幅广告在不同设备上占据的屏幕区域更为一致,也带来了提升广告效果的机会。
请注意,使用自适应横幅广告时,如果给定设备类型和宽度,则返回的广告尺寸将始终固定不变。在给定设备上测试布局后,便可以确定广告尺寸不会发生变化。不过,横幅广告素材的尺寸在不同设备上可能会有所不同。因此,建议您确保广告布局可以适应各种广告高度。 在极少数情况下,广告素材可能填不满整个自适应广告单元的尺寸,此时会改而展示一个标准尺寸的广告素材,并在广告位中居中显示它。
自适应横幅广告适用情形
自适应横幅广告可直接替换行业标准的 320x50 横幅广告尺寸及自适应横幅广告格式。
这些横幅广告尺寸通常用于锚定横幅广告,锚定横幅广告通常固定在屏幕的顶部或底部展示。对于此类锚定横幅广告,当使用自适应横幅广告时,宽高比与标准 320x50 广告大致相当,如以下三张屏幕截图所示:
自适应横幅广告可更好地利用可用的屏幕尺寸。此外,与智能横幅广告相比,自适应横幅广告是一种更好的选择,原因如下:
这种广告采用提供的宽度(而不是全屏宽度),这样您就可以将 安全区域考虑在内。
它会针对具体设备选择最优高度,而不是针对不同尺寸的设备都使用一个固定的高度,从而降低了设备屏幕尺寸多样造成的影响。
实现说明
在应用中植入自适应横幅广告时,请注意以下几点:
- 您必须知道要展示广告的视图的宽度,并且在确定视图宽度时应考虑到设备宽度,以及任何适用的安全区域 。
当较小尺寸的广告不能填充广告位时,请确保您的广告视图背景是不透明的,以符合AdMob 政策。
确保您使用的是最新版 Google 移动广告 SDK。 对于中介,请使用最新版中介适配器。
自适应横幅广告尺寸经过专门设计,占满可用宽度时效果最佳。在大多数情况下,这里指的是所用设备的屏幕全宽。请务必考虑适用的安全区域。
Google 移动广告 SDK 在
AdSize
中针对指定的宽度返回经过优化的广告高度。GADAdSize
。获取自适应横幅广告尺寸的方法有三种:一种适用于横向屏幕,一种适用于纵向屏幕,还有一个适用于执行操作时的屏幕方向。如需了解详情,请参阅下面的完整 API 文档。
在给定设备上针对给定宽度返回的广告尺寸始终相同,因此在给定设备上测试布局后,您可以确定广告尺寸不会发生变化。
锚定横幅广告的高度始终不会超过设备高度的 15%,也始终不小于 50点。
快速入门
要植入简单的自适应锚定横幅广告,请按以下步骤操作。
创建 a
GADBannerView
对象并设置广告单元 ID。获取自适应横幅广告尺寸。您获取的尺寸将用于请求自适应横幅广告。要获取自适应广告尺寸,请务必执行以下操作:
- 获取所用设备的宽度,或者自行设置宽度(如果您不想使用屏幕的全宽)。
- 针对广告尺寸类使用相应的静态方法(例如
GADCurrentOrientationAnchoredAdaptiveBannerAdSizeWithWidth(CGFloat width)
),以获取所选屏幕方向的自适应GADAdSize
对象。 - 在横幅广告视图中设置广告尺寸,可通过在
GADBannerView
上设置adSize
属性来实现。
下面列出了完整示例。
在预先准备的广告视图中,使用
loadRequest
方法创建广告请求对象并加载横幅广告,其处理方式与常规横幅广告请求一样。
示例代码
以下是一个视图控制器示例,该视图控制器能在任何 iOS 版本中加载和重新加载自适应横幅广告,因此能够将安全区域和视图方向考虑在内:
Swift
class ViewController: UIViewController {
@IBOutlet weak var bannerView: GADBannerView!
override func viewDidLoad() {
super.viewDidLoad()
// Step 1 - Create a GADBannerView
(in code or interface builder) and set the
// ad unit ID on it.
bannerView.adUnitID = "ca-app-pub-3940256099942544/2435281174"
bannerView.rootViewController = self
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
// Note loadBannerAd is called in viewDidAppear as this is the first time that
// the safe area is known. If safe area is not a concern (e.g., your app is
// locked in portrait mode), the banner can be loaded in viewWillAppear.
loadBannerAd()
}
override func viewWillTransition(to size: CGSize,
with coordinator: UIViewControllerTransitionCoordinator) {
super.viewWillTransition(to:size, with:coordinator)
coordinator.animate(alongsideTransition: { _ in
self.loadBannerAd()
})
}
func loadBannerAd() {
// Step 2 - Determine the view width to use for the ad width.
let frame = { () -> CGRect in
// Here safe area is taken into account, hence the view frame is used
// after the view has been laid out.
if #available(iOS 11.0, *) {
return view.frame.inset(by: view.safeAreaInsets)
} else {
return view.frame
}
}()
let viewWidth = frame.size.width
// Step 3 - Get Adaptive GADAdSize and set the ad view.
// Here the current interface orientation is used. If the ad is being preloaded
// for a future orientation change or different orientation, the function for the
// relevant orientation should be used.
bannerView.adSize = GADCurrentOrientationAnchoredAdaptiveBannerAdSizeWithWidth(viewWidth)
// Step 4 - Create an ad request and load the adaptive banner ad.
bannerView.load(GADRequest())
}
}
Objective-C
@implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // Step 1 - Create aGADBannerView
(in code or interface builder) and set the // ad unit ID on it. self.bannerView.adUnitID = @"ca-app-pub-3940256099942544/2435281174"; self.bannerView.rootViewController = self; } - (void)viewDidAppear:(BOOL)animated { [super viewDidAppear:animated]; // Note loadBannerAd is called in viewDidAppear as this is the first time that // the safe area is known. If safe area is not a concern (e.g., your app is // locked in portrait mode), the banner can be loaded in viewWillAppear. [self loadBannerAd]; } - (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id)coordinator { [super viewWillTransitionToSize:size withTransitionCoordinator:coordinator]; [coordinator animateAlongsideTransition:^(id _Nonnull context) { [self loadBannerAd]; } completion:nil]; } - (void)loadBannerAd { // Step 2 - Determine the view width to use for the ad width. CGRect frame = self.view.frame; // Here safe area is taken into account, hence the view frame is used after // the view has been laid out. if (@available(iOS 11.0, *)) { frame = UIEdgeInsetsInsetRect(self.view.frame, self.view.safeAreaInsets); } CGFloat viewWidth = frame.size.width; // Step 3 - Get Adaptive GADAdSize and set the ad view. // Here the current interface orientation is used. If the ad is being // preloaded for a future orientation change or different orientation, the // function for the relevant orientation should be used. self.bannerView.adSize = GADCurrentOrientationAnchoredAdaptiveBannerAdSizeWithWidth(viewWidth); // Step 4 - Create an ad request and load the adaptive banner ad. GADRequest *request = [GADRequest request]; [self.bannerView loadRequest:request]; } @end
在本示例中,函数 GADCurrentOrientationAnchoredAdaptiveBannerAdSizeWithWidth(CGFloat width)
用于获取当前界面方向的锚定位置上的横幅广告尺寸。要按给定屏幕方向预加载锚定横幅广告,请使用 GADPortraitAnchoredAdaptiveBannerAdSizeWithWidth(CGFloat width)
和 GADLandscapeAnchoredAdaptiveBannerAdSizeWithWidth(CGFloat width)
中的相关函数。
GitHub 上的完整示例
Swift | Objective-C |