繁体   English   中英

使用自动布局在情节提要上实现 ADBannerView

[英]Implementation of ADBannerView on storyboard with autolayout

我有 Xcode 6.3.2,我在情节提要上实现 ADBannerView 时遇到问题。 Xcode 一直显示警告:

“横幅视图”的框架在运行时会有所不同。

我添加了 3 个约束,你可以在下面看到它们。

所有的约束所有的约束水平居中水平居中底部空间到底部布局指南 = 0底部约束 前导空格 = 0在此处输入图片说明

如何正确实施横幅?

我不能使用“self.canDisplayBannerAds = true”,因为我还使用“bannerViewDidLoadAd”和“didFailToReceiveAdWithError”来调整内容大小,以及“bannerViewActionShouldBegin”和“bannerViewActionDidFinish”来暂停并重新启动应用程序活动。

解决了!

要在纵向和横向中使用自动布局和尺寸类添加 iAd 横幅但不使用canDisplayBannerAds首先声明横幅var bannerView: ADBannerView! .

使用它来设置委托并添加横幅以查看:

func loadAds() {
    bannerView = ADBannerView(adType: ADAdType.Banner)
    bannerView.hidden = true
    bannerView.delegate = self
    self.view.addSubview(bannerView)
}

使用以下代码让横幅随屏幕旋转并在 iAd 加载时调整屏幕内容contentView大小( bottomConstraint是从contentView到底部的约束):

override func viewDidLayoutSubviews() {
    self.layoutAnimated(UIView.areAnimationsEnabled())
}

func layoutAnimated(animated: Bool) {
    var contentFrame = self.view.bounds

    var sizeForBanner = bannerView.sizeThatFits(contentFrame.size)

    var bannerFrame = bannerView.frame
    if self.bannerView.bannerLoaded {

        contentFrame.size.height -= sizeForBanner.height
        bannerFrame.origin.y = contentFrame.size.height
        bannerFrame.size.height = sizeForBanner.height
        bannerFrame.size.width = sizeForBanner.width

        let verticalBottomConstraint = self.bottomConstraint
        verticalBottomConstraint.constant = sizeForBanner.height
        self.view.layoutSubviews()
        bannerView.hidden = false
    } else {
        bannerFrame.origin.y = contentFrame.size.height
        bannerView.hidden = true
        let verticalBottomConstraint = self.bottomConstraint
        verticalBottomConstraint.constant = 0
    }
    UIView.animateWithDuration(animated ? 0.25 : 0.0, animations: {
        self.contentView.layoutIfNeeded()
        self.bannerView.frame = bannerFrame
    })
}

在这里你调用上面的代码来在加载或加载 iAd 失败时显示和隐藏横幅

func bannerViewDidLoadAd(banner: ADBannerView!) {
    self.layoutAnimated(true)
}

func bannerView(banner: ADBannerView!, didFailToReceiveAdWithError error: NSError!) {
    self.layoutAnimated(true)
}

现在您可以使用bannerViewActionShouldBeginbannerViewActionDidFinish来暂停和启动您的应用活动。 :)

我相信这是ADBannerView的自动布局问题。 我以类似的方式实现了我的ADBannerView ,但我一直无法找到一种方法来消除警告,同时满足我的ADBannerView所需的灵活性。 以完全相同的方式实施 AdMob GADBannerView不会导致出现自动布局警告。 正如你在我的屏幕截图中看到的, ADBannerView确实会在设备屏幕上拉伸, width = 600 ,但自动布局仍然认为它是默认大小, width = 480

此时最好的答案是简单地忽略警告。

ADBannerViewAutoLayout

全屏

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM