如何在底部中心放置iOS的Admob横幅?

时间:2015-03-12 14:47:37

标签: ios objective-c admob

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

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

如何将横幅定位在底部中心?

2 个答案:

答案 0 :(得分:5)

您只需在初始化后将其添加为子视图:

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_];

答案 1 :(得分:0)

经过反复试验,这段代码帮助我将横幅广告置于底部。 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)]
        )

    }