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

PawełRup

我有Xcode 6.3.2,并且在情节提要板上实现ADBannerView时遇到问题。Xcode始终显示警告:

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

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

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

如何正确实施标语?

I can not use "self.canDisplayBannerAds = true" because I also use "bannerViewDidLoadAd" and "didFailToReceiveAdWithError" to resize content and also "bannerViewActionShouldBegin" and "bannerViewActionDidFinish" to pause and start again application activity.

Paweł Rup

SOLVED!

To add iAd banner using Auto Layout and Size Classes in portrait and landscape but without using canDisplayBannerAds first declare banner var bannerView: ADBannerView!.

Use this to set delegate and add banner to view:

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

Use following code to let banner rotate with screen and resize screen content contentView when iAd loads (bottomConstraint is a constraint from contentView to bottom):

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
    })
}

Here you call above code to show and hide banner when loads or failed to load iAd

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

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

现在,您可以使用bannerViewActionShouldBeginbannerViewActionDidFinish暂停并开始您的应用活动。:)

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何使用自动布局为情节提要板调整图像大小?

如何在情节提要中的按钮上实现此布局?

使用2个不同的情节提要板,一个用于LTR,一个用于RTL(非自动布局)

使ScrollView与自动布局和情节提要一起使用

在UICollectionViewCell中使用情节提要中的自动布局约束

Xcode 6.3情节提要自动布局约束

iOS9情节提要自动布局

Xcode 8 GM种子情节提要板布局问题

情节提要板可以自动更改方向吗?

Xcode 9情节提要板窗口位置自动保存

使用情节提要板实现自定义视图布局

使用自适应布局和情节提要在XCode6中实现ScrollView

使用情节提要中的自动布局在景观中更改视框

tableHeaderView使用自动布局和情节提要以全高显示

使用自动布局和情节提要(不带XIB)的UICollectionViewCell的可变宽度和高度

如何使用自动布局从情节提要中动态获得视图的y位置

查找情节提要布局及其相关代码实现的简便方法?

通用情节提要板上的字体大小

移动不在屏幕上的情节提要板元素

视图控制器在情节提要板上显示空白

Xcode在打开情节提要板上崩溃

使用情节提要板时出现带SceneDelegate的黑屏

使用多个情节提要板进行状态恢复

WPF使用情节提要板放大矩形

使用情节提要板时修改UITabBarController

在ToolTip WPF上使用情节提要

混合情节提要板原型单元格与自动填充数据

检索框架外部的情节提要板元素

VisualState情节提要板更改GridView方向