根据横幅视图的外观调整布局大小

时间:2015-08-03 21:12:21

标签: ios swift admob iad

我有一个应用程序,目前已在AppStore上发布。现在我想改变我的广告策略。我正在尝试实现像“show iAd,如果它失败显示admob”这样的算法 我按照代码iAd Admob mediation with singleton 我在下面的实现有问题。除了横幅视图覆盖我的主视图外,一切似乎都很好。正如我之前所说,这个应用程序到目前为止已分发,所以我试图这样做而不改变我以前的布局和自动布局约束。我希望我之前的视图会根据横幅视图的外观自动调整大小。当横幅出现时,如果横幅无法获得任何广告,主视图应缩小然后放大。

它覆盖了我的主要视图元素。 It overlays my main view elements

AppDelegate.swift

var iAdBanner = ADBannerView()
var adMobBanner = GADBannerView(adSize: kGADAdSizeBanner)

//Banner View Code

func bannerViewDidLoadAd(banner: ADBannerView!) {

    UIView.beginAnimations(nil, context: nil)
    iAdBanner.alpha = 1.0
    adMobBanner.alpha = 0.0
    UIView.commitAnimations()
    NSLog("iAd loaded ad")
}

func bannerView(banner: ADBannerView!, didFailToReceiveAdWithError error: NSError!) {


    UIView.beginAnimations(nil, context: nil)
    iAdBanner.alpha = 0.0
    adMobBanner.alpha = 1.0
    UIView.commitAnimations()
    NSLog("iAd failed to load ad")

}

func adView(view: GADBannerView!, didFailToReceiveAdWithError error: GADRequestError!) {

    println("We dont have admob hide it!")

    adMobBanner.alpha = 0
}

func adViewDidReceiveAd(view: GADBannerView!) {

    if iAdBanner.alpha == 0.0 {
        println("No IAD show admob")
        adMobBanner.alpha = 1.0

    }
}

Registration.swift

class Registration: NSObject {

var appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate //non-optional variable
static var sharedInstance = Registration()

func showAds(viewController:UIViewController, view:UIView?) {
    var bannerView:UIView = view ?? viewController.view

    //Admob
    appDelegate.adMobBanner.rootViewController = viewController
    appDelegate.adMobBanner.delegate = appDelegate
    var request = GADRequest()
    appDelegate.adMobBanner.adUnitID = "ca-app"
    appDelegate.adMobBanner.loadRequest(request)
    appDelegate.adMobBanner.setTranslatesAutoresizingMaskIntoConstraints(false)
    bannerView.addSubview(appDelegate.adMobBanner)
    var myConstraint = NSLayoutConstraint(item: appDelegate.adMobBanner, attribute: NSLayoutAttribute.Leading, relatedBy: NSLayoutRelation.Equal, toItem: bannerView, attribute: NSLayoutAttribute.Leading, multiplier: 1.0, constant: 0)
    bannerView.addConstraint(myConstraint)
    myConstraint = NSLayoutConstraint(item: appDelegate.adMobBanner, attribute: NSLayoutAttribute.Trailing, relatedBy: NSLayoutRelation.Equal, toItem: bannerView, attribute: NSLayoutAttribute.Trailing, multiplier: 1.0, constant: 0)
    bannerView.addConstraint(myConstraint)
    myConstraint = NSLayoutConstraint(item: appDelegate.adMobBanner, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: bannerView, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 0)
    bannerView.addConstraint(myConstraint)

    // iAd
    appDelegate.iAdBanner.delegate = appDelegate;
    appDelegate.iAdBanner.setTranslatesAutoresizingMaskIntoConstraints(false)
    bannerView.addSubview(appDelegate.iAdBanner)
    myConstraint = NSLayoutConstraint(item: appDelegate.iAdBanner, attribute: NSLayoutAttribute.Leading, relatedBy: NSLayoutRelation.Equal, toItem: bannerView, attribute: NSLayoutAttribute.Leading, multiplier: 1.0, constant: 0)
    bannerView.addConstraint(myConstraint)

    myConstraint = NSLayoutConstraint(item: appDelegate.iAdBanner, attribute: NSLayoutAttribute.Trailing, relatedBy: NSLayoutRelation.Equal, toItem: bannerView, attribute: NSLayoutAttribute.Trailing, multiplier: 1.0, constant: 0)
    bannerView.addConstraint(myConstraint)

    myConstraint = NSLayoutConstraint(item: appDelegate.iAdBanner, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: bannerView, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 0)
    bannerView.addConstraint(myConstraint)

}

我在主视图控制器中显示了我的广告,例如:

    override func viewDidAppear(animated: Bool) {
    super.viewDidAppear(animated)
    Registration.sharedInstance.showAds(self,view:nil)
}

2 个答案:

答案 0 :(得分:0)

您可以创建一个UIView,比如说bannerAdView将其大小设置为您想要的大小,并在该视图中显示您的iAd或Admob。为iAdBanner和adMobBanner提供与bannerAdView's bounds

相同的帧大小

答案 1 :(得分:0)

您问题的简单解决方案是使用self.canDisplayBannerAds = true。只需将其添加到viewDidLoad即可。以这种方式实施您的iAd横幅广告会在广告可用时调整UIView的大小,并在iAd横幅不可用时将其自身移出屏幕。删除您ADBannerView的当前实现并尝试一下。但是使用self.canDisplayBannerAds = true有一些缺点,比如无法使用ADBannerView的委托方法。我假设您可以检查UIView是否包含ADBannerView,如果您愿意,可以据此采取相应措施。