Skip to content

Commit 90e2ffa

Browse files
malandr2copybara-github
authored andcommitted
Update inline adaptive snippets
PiperOrigin-RevId: 868207160
1 parent 1e7019d commit 90e2ffa

File tree

4 files changed

+16
-68
lines changed

4 files changed

+16
-68
lines changed

Objective-C/advanced/APIDemo/APIDemo/Snippets/AdManagerBannerSnippets.m

Lines changed: 4 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -67,28 +67,11 @@ - (void)loadInlineAdaptiveBanner {
6767
GAMBannerView *bannerView = self.bannerView;
6868
UIView *view = self.view;
6969

70-
// [START get_width]
71-
CGFloat totalWidth = CGRectGetWidth(view.bounds);
70+
// [START set_inline_adaptive_ad_size]
7271
// Make sure the ad fits inside the readable area.
73-
UIEdgeInsets insets = view.safeAreaInsets;
74-
CGFloat adWidth = totalWidth - insets.left - insets.right;
75-
// [END get_width]
76-
77-
// View is not laid out yet, return early.
78-
if (adWidth <= 0) {
79-
return;
80-
}
81-
82-
// [START set_adaptive_ad_size]
83-
GADAdSize adSize = GADCurrentOrientationInlineAdaptiveBannerAdSizeWithWidth(adWidth);
84-
bannerView.adSize = adSize;
85-
86-
// For Ad Manager, the `adSize` property is used for the adaptive banner ad
87-
// size. The `validAdSizes` property is used as normal for the supported
88-
// reservation sizes for the ad placement.
89-
GADAdSize validAdSize = GADLargeAnchoredAdaptiveBannerAdSizeWithWidth(adWidth);
90-
bannerView.validAdSizes = @[ NSValueFromGADAdSize(validAdSize) ];
91-
// [END set_adaptive_ad_size]
72+
CGFloat adWidth = CGRectGetWidth(UIEdgeInsetsInsetRect(view.bounds, view.safeAreaInsets));
73+
bannerView.adSize = GADCurrentOrientationInlineAdaptiveBannerAdSizeWithWidth(adWidth);
74+
// [END set_inline_adaptive_ad_size]
9275

9376
// Test ad unit ID for inline adaptive banners.
9477
bannerView.adUnitID = kTestAdUnitID;

Objective-C/advanced/APIDemo/APIDemo/Snippets/BannerSnippets.m

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -76,22 +76,11 @@ - (void)loadInlineAdaptiveBanner {
7676
GADBannerView *bannerView = self.bannerView;
7777
UIView *view = self.view;
7878

79-
// [START get_width]
80-
CGFloat totalWidth = CGRectGetWidth(view.bounds);
79+
// [START set_inline_adaptive_ad_size]
8180
// Make sure the ad fits inside the readable area.
82-
UIEdgeInsets insets = view.safeAreaInsets;
83-
CGFloat adWidth = totalWidth - insets.left - insets.right;
84-
// [END get_width]
85-
86-
// View is not laid out yet, return early.
87-
if (adWidth <= 0) {
88-
return;
89-
}
90-
91-
// [START set_adaptive_ad_size]
92-
GADAdSize adSize = GADCurrentOrientationInlineAdaptiveBannerAdSizeWithWidth(adWidth);
93-
bannerView.adSize = adSize;
94-
// [END set_adaptive_ad_size]
81+
CGFloat adWidth = CGRectGetWidth(UIEdgeInsetsInsetRect(view.bounds, view.safeAreaInsets));
82+
bannerView.adSize = GADCurrentOrientationInlineAdaptiveBannerAdSizeWithWidth(adWidth);
83+
// [END set_inline_adaptive_ad_size]
9584

9685
// Test ad unit ID for inline adaptive banners.
9786
bannerView.adUnitID = kTestAdUnitID;

Swift/advanced/APIDemo/APIDemo/Snippets/AdManagerBannerSnippets.swift

Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -54,26 +54,11 @@ private class AdManagerBannerSnippets: UIViewController, AppEventDelegate, Banne
5454
}
5555

5656
private func loadInlineAdaptiveBanner() {
57-
// [START get_width]
58-
let totalWidth = view.bounds.width
57+
// [START set_inline_adaptive_ad_size]
5958
// Make sure the ad fits inside the readable area.
60-
let insets = view.safeAreaInsets
61-
let adWidth = totalWidth - insets.left - insets.right
62-
// [END get_width]
63-
64-
// View is not laid out yet, return early.
65-
guard adWidth > 0 else { return }
66-
67-
// [START set_adaptive_ad_size]
68-
let adSize = currentOrientationInlineAdaptiveBanner(width: adWidth)
69-
bannerView.adSize = adSize
70-
71-
// For Ad Manager, the `adSize` property is used for the adaptive banner ad
72-
// size. The `validAdSizes` property is used as normal for the supported
73-
// reservation sizes for the ad placement.
74-
let validAdSize = largeAnchoredAdaptiveBanner(width: adWidth)
75-
bannerView.validAdSizes = [nsValue(for: validAdSize)]
76-
// [END set_adaptive_ad_size]
59+
let adWidth = view.bounds.inset(by: view.safeAreaInsets).width
60+
bannerView.adSize = currentOrientationInlineAdaptiveBanner(width: adWidth)
61+
// [END set_inline_adaptive_ad_size]
7762

7863
// Test ad unit ID for inline adaptive banners.
7964
bannerView.adUnitID = testAdUnitID

Swift/advanced/APIDemo/APIDemo/Snippets/BannerSnippets.swift

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -64,20 +64,11 @@ private class BannerSnippets: UIViewController, BannerViewDelegate {
6464
}
6565

6666
private func loadInlineAdaptiveBanner() {
67-
// [START get_width]
68-
let totalWidth = view.bounds.width
67+
// [START set_inline_adaptive_ad_size]
6968
// Make sure the ad fits inside the readable area.
70-
let insets = view.safeAreaInsets
71-
let adWidth = totalWidth - insets.left - insets.right
72-
// [END get_width]
73-
74-
// View is not laid out yet, return early.
75-
guard adWidth > 0 else { return }
76-
77-
// [START set_adaptive_ad_size]
78-
let adSize = currentOrientationInlineAdaptiveBanner(width: adWidth)
79-
bannerView.adSize = adSize
80-
// [END set_adaptive_ad_size]
69+
let adWidth = view.bounds.inset(by: view.safeAreaInsets).width
70+
bannerView.adSize = currentOrientationInlineAdaptiveBanner(width: adWidth)
71+
// [END set_inline_adaptive_ad_size]
8172

8273
// Test ad unit ID for inline adaptive banners.
8374
bannerView.adUnitID = testAdUnitID

0 commit comments

Comments
 (0)