如何将 iOS 的 Admob 横幅放在底部中央?

How to put Admob banner for iOS on bottom centre?

我正在使用此代码将横幅广告放在左下角。

CGPoint origin = CGPointMake(0.0,
                             self.view.frame.size.height -
                             CGSizeFromGADAdSize(kGADAdSizeBanner).height);
bannerView_ = [[GADBannerView alloc] initWithAdSize:kGADAdSizeBanner
                                             origin:origin];

如何将横幅置于底部居中?

初始化后添加为子视图即可:

CGPoint origin = CGPointMake(0.0, self.view.frame.size.height - CGSizeFromGADAdSize(kGADAdSizeBanner).height);

bannerView_ = [[GADBannerView alloc] initWithAdSize:kGADAdSizeBanner
                                                         origin:origin];
bannerView_.center = CGPointMake(self.view.center.x, self.view.frame.size.height-CGSizeFromGADAdSize(kGADAdSizeBanner).height/2)
[self.view addSubview: bannerView_];

经过反复试验,此代码帮助我将横幅广告居中放置在底部。 Google admob 文档也有帮助。

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

    }