Google AdMob (iOS)バナー広告のサンプルコードで「bottomLayoutGuide」の警告が出る件

https://developers.google.com/admob/ios/banner

2020/04/29 時点で、サンプルコードが以下になっているが

func addBannerViewToView(_ bannerView: GADBannerView) {
    bannerView.translatesAutoresizingMaskIntoConstraints = false
    view.addSubview(bannerView)
    view.addConstraints(
      [NSLayoutConstraint(item: bannerView,
                          attribute: .bottom,
                          relatedBy: .equal,
                          toItem: bottomLayoutGuide,
                          attribute: .top,
                          multiplier: 1,
                          constant: 0),
       NSLayoutConstraint(item: bannerView,
                          attribute: .centerX,
                          relatedBy: .equal,
                          toItem: view,
                          attribute: .centerX,
                          multiplier: 1,
                          constant: 0)
      ])
   }


bottomLayoutGuide

'bottomLayoutGuide' was deprecated in iOS 11.0: Use view.safeAreaLayoutGuide.bottomAnchor instead of bottomLayoutGuide.topAnchor

の警告が表示されるとおり iOS 11 以降では非推奨なので、以下のように safeAreaLayoutGuide を使用すると警告が出なくなる。


この時、気を付けたいのは bottomLayoutGuide -> view.safeAreaLayoutGuide だとアプリ実行時に crash するため、 同時に attribute.bottom -> .top に変更する必要もある。

      [NSLayoutConstraint(item: bannerView,
                          attribute: .bottom,
                          relatedBy: .equal,
                          toItem: view.safeAreaLayoutGuide, // bottomLayoutGuide
                          attribute: .bottom, // top
                          multiplier: 1,
                          constant: 0),