[英]XCODE / SWIFT - reposition ADMOB banner to the bottom
i am having trouble reposition my admob banner at the bottom of the screen ,no matter what i have tried it stay at the top .. i found out alot of this kind of questions, i have tried what they did and nothing work.我无法将我的 admob 横幅重新定位在屏幕底部,无论我尝试过什么,它都停留在顶部.. 我发现了很多此类问题,我已经尝试过他们所做的,但没有任何效果。 Here is what im trying :
这是我正在尝试的内容:
bannerView = GADBannerView(adSize: kGADAdSizeBanner)
bannerView?.adUnitID = "ca-app-pub-466066799532826501/8972705271"
bannerView?.delegate = self
bannerView?.rootViewController = self
let screenRect = UIScreen.mainScreen().bounds
let screenWidth = screenRect.size.width
let screenHeight = screenRect.size.height
let screenXPos = (screenWidth / 2)
let screenYPos = screenHeight - kGADAdSizeBanner.size.height
bannerView!.frame = CGRectMake(screenWidth/2.0 - bannerView!.frame.size.width/2.0, screenHeight - bannerView!.frame.size.height,
bannerView!.frame.size.width,bannerView!.frame.size.height);
self.view.addSubview(bannerView!)
bannerView?.loadRequest(GADRequest())
timerAd?.invalidate()
timerAd = NSTimer.scheduledTimerWithTimeInterval(40, target: self, selector: "GoogleAdRequestTimer", userInfo: nil, repeats: true)
This is how i do it:这就是我的做法:
func adViewDidReceiveAd(view: GADBannerView!) {
println("adViewDidReceiveAd:\(view)");
bannerDisplayed = true
relayoutViews()
}
func relayoutViews() {
if (bannerDisplayed) {
var bannerFrame = bannerView!.frame
bannerFrame.origin.x = 0
bannerFrame.origin.y = screenHeight - bannerFrame.size.height
bannerView!.frame = bannerFrame
// and so on
The GoogleAds GitHub has an example : GoogleAds GitHub 有一个例子:
bannerView = GADBannerView(adSize: kGADAdSizeSmartBannerPortrait)
bannerView.adUnitID = "*******YOUR CA-PUB-ID*******"
bannerView.rootViewController = self
let request = GADRequest()
request.testDevices = [ kGADSimulatorID, "********* YOUR SIMULATOR ID*******" ]
self.view.addSubview(bannerView)
bannerView.translatesAutoresizingMaskIntoConstraints = false
view.addConstraint(NSLayoutConstraint(item: bannerView, attribute: .bottom,
relatedBy: .equal, toItem: view, attribute: .bottom, multiplier: 1, constant: 0))
view.addConstraint(NSLayoutConstraint(item: bannerView, attribute: .centerX,
relatedBy: .equal, toItem: view, attribute: .centerX, multiplier: 1, constant: 0))
bannerView.load(request)
After the normal setup then add the constraints.在正常设置之后,然后添加约束。 This got me what I wanted positioning the banner ad to the bottom center.
这让我得到了我想要将横幅广告定位到底部中心的东西。
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)]
)
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.