自適應橫幅是下一代響應式廣告,通過優化每個設備的廣告尺寸來最大化性能。對僅支持固定高度的智能橫幅進行了改進,自適應橫幅讓開發人員可以指定廣告寬度並使用它來確定最佳廣告尺寸。
為了選擇最佳的廣告尺寸,自適應橫幅使用固定的縱橫比而不是固定的高度。這導致橫幅廣告在不同設備的屏幕上佔據更一致的部分,並提供了提高性能的機會。
使用自適應橫幅時,請注意這些將始終為給定的設備和寬度返回一個恆定的大小。在給定設備上測試佈局後,您可以確定廣告尺寸不會改變。但是,橫幅廣告素材的大小可能會因不同設備而異。因此,建議確保您的佈局可以適應廣告高度的變化。在極少數情況下,可能不會填充完整的自適應尺寸,而是將標準尺寸的廣告素材置於此廣告位的中心。
何時使用自適應橫幅
自適應橫幅旨在替代行業標準 320x50 橫幅尺寸以及它們取代的智能橫幅格式。
這些橫幅尺寸通常用作錨定橫幅,通常鎖定在屏幕的頂部或底部。對於此類錨定橫幅,使用自適應橫幅時的縱橫比將類似於標準 320x50 廣告的縱橫比,如以下屏幕截圖所示:
自適應橫幅可以更好地利用可用的屏幕尺寸。此外,與智能橫幅相比,自適應橫幅是更好的選擇,因為:
它使用提供的寬度而不是全屏寬度,使您能夠考慮 安全區域。
它為特定設備選擇最佳高度,而不是在不同大小的設備之間保持恆定的高度,從而減輕設備碎片的影響。
實施說明
在您的應用中實施自適應橫幅時,請記住以下幾點:
- 您必須知道將放置廣告的視圖的寬度,這應該考慮到設備寬度和任何適用安全區域 。
確保您的廣告視圖背景不透明以符合AdMob 政策,當投放的較小的廣告尺寸未填滿廣告位時。
確保您使用的是最新版本的 Google 移動廣告 SDK。對於中介,請使用最新版本的中介適配器。
自適應橫幅尺寸旨在在使用完整可用寬度時發揮最佳效果。在大多數情況下,這將是正在使用的設備屏幕的整個寬度。請務必考慮適用安全區域。
Google 移動廣告 SDK 針對 一個
GADAdSize
。有三種方法可以獲取自適應橫幅的廣告尺寸——一種用於橫向,一種用於縱向,另一種用於執行時的當前方向。有關更多信息,請參閱下面的完整 API 文檔。
在給定設備上為給定寬度返回的尺寸將始終相同,因此一旦您在給定設備上測試了佈局,您就可以確定廣告尺寸不會改變。
錨定橫幅高度永遠不會大於設備高度的 15%,也永遠不會小於 50點。
快速開始
按照以下步驟實現簡單的自適應錨橫幅。
創建 a
GADBannerView
對象並設置您的廣告單元 ID。獲取自適應橫幅廣告尺寸。您獲得的尺寸將用於請求您的自適應橫幅。要獲得自適應廣告尺寸,請確保您:
- 獲取正在使用的設備的寬度,或者如果您不想使用屏幕的整個寬度,請設置自己的寬度。
- 對廣告尺寸類使用適當的靜態方法,例如
GADCurrentOrientationAnchoredAdaptiveBannerAdSizeWithWidth(CGFloat width)
以獲得自適應GADAdSize
所選方向的對象。 - 在橫幅廣告視圖上設置廣告尺寸 - 使用
AdView.setAdSize()
執行此通過在GADBannerView
上設置adSize
屬性。
下面是一個完整的例子。
創建一個廣告請求對象並在準備好的廣告視圖上使用
loadRequest
方法加載您的橫幅,就像使用普通橫幅請求一樣。
示例代碼
這是一個視圖控制器示例,它將在任何 iOS 版本上加載和重新加載自適應橫幅,同時考慮安全區域和視圖方向:
迅速
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 上的完整範例
快速 | Objective-C |