רינדור מודעות ב-iPhone X

במדריך הזה מפורטות שיטות מומלצות לקידוד האפליקציות כך שהמודעות יוצגו בצורה נכונה באייפון X.

דרישות מוקדמות

צריך למקם מודעות באנר ב"אזור הבטוח" כדי למנוע את הסתרתן על ידי פינות מעוגלות, בתי חיישנים והאינדיקטור של כפתור הבית. בדף הזה תמצאו דוגמאות להוספת מגבלות כדי למקם מודעת באנר בחלק העליון או התחתון של 'האזור הבטוח'.

סטוריבורד/כלי ליצירת ממשקים

אם האפליקציה שלכם משתמשת ב-Interface Builder, קודם צריך לוודא שהפעלתם את ההגדרות של Safe Area layout guides. כדי לעשות את זה, צריך להשתמש ב-Xcode בגרסה 9 ואילך ולטרגט מכשירים עם iOS בגרסה 9 ואילך.

פותחים את הקובץ Interface Builder ולוחצים על סצנת בקר התצוגה. משמאל יופיעו האפשרויות של Interface Builder Document. מסמנים את האפשרות Use Safe Area Layout Guides ומוודאים שאתם יוצרים את האפליקציה ל-iOS 9.0 ואילך לפחות.

מומלץ להגביל את גודל הבאנר לגודל הנדרש באמצעות הגבלות רוחב וגובה.

עכשיו אפשר ליישר את הבאנר לחלק העליון של האזור הבטוח על ידי הגבלת המאפיין Top של GADBannerView לחלק העליון של האזור הבטוח:

באופן דומה, אפשר ליישר את הבאנר לחלק התחתון של האזור הבטוח על ידי הגבלת המאפיין Bottom של GADBannerView לחלק התחתון של האזור הבטוח:

האילוצים שלכם אמורים להיראות עכשיו כמו בצילום המסך שלמטה (הגודל והמיקום יכולים להיות שונים):

ViewController

הנה קטע קוד פשוט של בקר תצוגה שמבצע את הפעולות המינימליות שנדרשות כדי להציג באנר ב-GADBannerView כפי שהוגדר בלוח התכנון שלמעלה:

Swift

class ViewController: UIViewController {

  /// The banner view.
  @IBOutlet var bannerView: BannerView!

  override func viewDidLoad() {
    super.viewDidLoad()
    // Replace this ad unit ID with your own ad unit ID.
    bannerView.adUnitID = "ca-app-pub-3940256099942544/2934735716"
    bannerView.rootViewController = self
    bannerView.load(Request())
  }

}

Objective-C

@interface ViewController()

@property(nonatomic, strong) IBOutlet GADBannerView *bannerView;

@end

@implementation ViewController

- (void)viewDidLoad {
  [super viewDidLoad];

  // Replace this ad unit ID with your own ad unit ID.
  self.bannerView.adUnitID = @"ca-app-pub-3940256099942544/2934735716";
  self.bannerView.rootViewController = self;
  GADRequest *request = [GADRequest request];
  [self.bannerView loadRequest:request];
}

התאמת הבאנרים לקצה של האזור הבטוח

אם רוצים שהבאנר יהיה מיושר לימין או לשמאל, צריך להגביל את הקצה הימני או השמאלי של הבאנר לקצה הימני או השמאלי של האזור הבטוח, ולא לקצה הימני או השמאלי של תצוגת העל.

אם האפשרות Use Safe Area Layout Guides (שימוש בקווי עזר לפריסה באזור בטוח) מופעלת, כלי בניית הממשק ישתמש כברירת מחדל בקצוות של האזור הבטוח כשמוסיפים אילוצים לתצוגה.

פרסום פרוגרמטי

אם האפליקציה שלכם יוצרת מודעות באנר באופן פרוגרמטי, אתם יכולים להגדיר אילוצים ולמקם את מודעת הבאנר בקוד. בדוגמה הזו מוצג איך להגביל את מודעת הבאנר כך שהיא תמוקם במרכז האופקי בחלק התחתון של האזור הבטוח:

Swift

class ViewController: UIViewController {

  var bannerView: BannerView!

  override func viewDidLoad() {
    super.viewDidLoad()

    // Instantiate the banner view with your desired banner size.
    bannerView = BannerView(adSize: AdSizeBanner)
    addBannerViewToView(bannerView)
    bannerView.rootViewController = self
    // Set the ad unit ID to your own ad unit ID here.
    bannerView.adUnitID = "ca-app-pub-3940256099942544/2934735716"
    bannerView.load(Request())
  }

  func addBannerViewToView(_ bannerView: UIView) {
    bannerView.translatesAutoresizingMaskIntoConstraints = false
    view.addSubview(bannerView)
    if #available(iOS 11.0, *) {
      positionBannerAtBottomOfSafeArea(bannerView)
    }
    else {
      positionBannerAtBottomOfView(bannerView)
    }
  }

  @available (iOS 11, *)
  func positionBannerAtBottomOfSafeArea(_ bannerView: UIView) {
    // Position the banner. Stick it to the bottom of the Safe Area.
    // Centered horizontally.
    let guide: UILayoutGuide = view.safeAreaLayoutGuide

    NSLayoutConstraint.activate(
      [bannerView.centerXAnchor.constraint(equalTo: guide.centerXAnchor),
       bannerView.bottomAnchor.constraint(equalTo: guide.bottomAnchor)]
    )
  }

  func positionBannerAtBottomOfView(_ bannerView: UIView) {
    // Center the banner horizontally.
    view.addConstraint(NSLayoutConstraint(item: bannerView,
                                          attribute: .centerX,
                                          relatedBy: .equal,
                                          toItem: view,
                                          attribute: .centerX,
                                          multiplier: 1,
                                          constant: 0))
    // Lock the banner to the top of the bottom layout guide.
    view.addConstraint(NSLayoutConstraint(item: bannerView,
                                          attribute: .bottom,
                                          relatedBy: .equal,
                                          toItem: self.bottomLayoutGuide,
                                          attribute: .top,
                                          multiplier: 1,
                                          constant: 0))
  }

}

Objective-C

@interface ViewController()

@property(nonatomic, strong) GADBannerView *bannerView;

@end

@implementation ViewController

- (void)viewDidLoad {
  [super viewDidLoad];

  // Instantiate the banner view with your desired banner size.
  self.bannerView = [[GADBannerView alloc] initWithAdSize:GADAdSizeBanner];
  [self addBannerViewToView:self.bannerView];

  // Replace this ad unit ID with your own ad unit ID.
  self.bannerView.adUnitID = @"ca-app-pub-3940256099942544/2934735716";
  self.bannerView.rootViewController = self;
  GADRequest *request = [GADRequest request];
  [self.bannerView loadRequest:request];
}

#pragma mark - view positioning

-(void)addBannerViewToView:(UIView *_Nonnull)bannerView {
  self.bannerView.translatesAutoresizingMaskIntoConstraints = NO;
  [self.view addSubview:self.bannerView];
  if (@available(ios 11.0, *)) {
    [self positionBannerViewAtBottomOfSafeArea:bannerView];
  } else {
    [self positionBannerViewAtBottomOfView:bannerView];
  }
}

- (void)positionBannerViewAtBottomOfSafeArea:(UIView *_Nonnull)bannerView NS_AVAILABLE_IOS(11.0) {
  // Position the banner. Stick it to the bottom of the Safe Area.
  // Centered horizontally.
  UILayoutGuide *guide = self.view.safeAreaLayoutGuide;
  [NSLayoutConstraint activateConstraints:@[
    [bannerView.centerXAnchor constraintEqualToAnchor:guide.centerXAnchor],
    [bannerView.bottomAnchor constraintEqualToAnchor:guide.bottomAnchor]
  ]];
}

- (void)positionBannerViewAtBottomOfView:(UIView *_Nonnull)bannerView {
  [self.view addConstraint:[NSLayoutConstraint constraintWithItem:bannerView
                                                        attribute:NSLayoutAttributeCenterX
                                                        relatedBy:NSLayoutRelationEqual
                                                           toItem:self.view
                                                        attribute:NSLayoutAttributeCenterX
                                                       multiplier:1
                                                         constant:0]];
  [self.view addConstraint:[NSLayoutConstraint constraintWithItem:bannerView
                                                        attribute:NSLayoutAttributeBottom
                                                        relatedBy:NSLayoutRelationEqual
                                                           toItem:self.bottomLayoutGuide
                                                        attribute:NSLayoutAttributeTop
                                                       multiplier:1
                                                         constant:0]];
}

@end

אפשר להשתמש בקלות בטכניקות שלמעלה כדי להגביל את התצוגה לחלק העליון של האזור הבטוח, על ידי שינוי המאפיינים והעוגנים שבהם נעשה שימוש.

מודעות באנר חכמות

אם אתם משתמשים בבאנרים חכמים, במיוחד במצב לרוחב, מומלץ להשתמש באילוצים כדי ליישר את קצוות הבאנר לקצוות השמאלי והימני של האזור הבטוח.

ב-Interface Builder, האפשרות הזו נתמכת עד iOS 9. כדי להשתמש בה, צריך לסמן את האפשרות Use Safe Area Layout Guides כמו שמתואר למעלה.

בקוד, כדאי להגדיר את אילוצי הקצה ביחס לקווי המתאר של האזור הבטוח, אם הם זמינים. קטע הקוד הבא מוסיף תצוגת באנר לתצוגה ומגביל אותה לתחתית התצוגה, לרוחב מלא:

Swift

func addBannerViewToView(_ bannerView: BannerView) {
  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]];
}

מודעות מותאמות

אם באפליקציה שלכם מוצמדות מודעות מותאמות לחלק העליון או התחתון של המסך, אותם העקרונות חלים על מודעות מותאמות כמו על מודעות באנר. ההבדל העיקרי הוא שבמקום להוסיף אילוצים ל-GADBannerView, צריך להוסיף אילוצים ל-GADNativeAdView (או לתצוגה שמכילה את המודעה) כדי לפעול בהתאם להנחיות הפריסה של האזור הבטוח. לגבי תצוגות מותאמות, מומלץ לספק הגבלות גודל מפורטות יותר.

מודעות מעברון ומודעות מתגמלות

החל מגרסה 7.26.0, ‏ Google Mobile Ads SDK תומך באופן מלא בפורמטים של מודעות מעברון ומודעות וידאו מתגמלות לאייפון X.