gpt4 book ai didi

ios - 根据横幅 View 的外观调整布局

转载 作者:行者123 更新时间:2023-11-28 09:01:49 26 4
gpt4 key购买 nike

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

它覆盖了我的主要 View 元素。 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)

}

我在主视图 Controller 中展示了我的广告,如下所示:

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

最佳答案

您可以创建单个 UIView,假设您将其命名为 bannerAdView 并根据需要设置其大小,并在该 View 中显示您的 iAd 或 Admob。为 iAdBanner 和 adMobBanner 提供与 bannerAdView 的边界相同的框架大小

关于ios - 根据横幅 View 的外观调整布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31796845/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com