【问题标题】:Swift admob hide when i re-open the app当我重新打开应用程序时 Swift admob 隐藏
【发布时间】:2015-10-27 13:54:16
【问题描述】:

我有一个带有横幅视图的视图控制器,我在那里集成了一个 admob 广告,我按照官方教程进行操作,但每次我关闭(不是多任务处理)并打开应用程序时它都会隐藏。为什么 ?我在 viewdidload 中添加了这个:

 bannerView.adUnitID = "ca-app-pub-"
    bannerView.rootViewController = self
    let request = GADRequest()
    request.testDevices = [ kGADSimulatorID ]
    bannerView.loadRequest(request)

编辑

当我在控制台中运行应用程序时:

    Google Mobile Ads SDK version: afma-sdk-i-v7.5.2
2015-10-27 15:16:01.146 Chat for WhatsApp[25348:2400768] Unable to simultaneously satisfy constraints.
    Probably at least one of the constraints in the following list is one you don't want. 
    Try this: 
        (1) look at each constraint and try to figure out which you don't expect; 
        (2) find the code that added the unwanted constraint or constraints and fix it. 
(
    "<NSLayoutConstraint:0x7fb9a4132e00 GADBannerView:0x7fb9a2f128e0.leading == UIView:0x7fb9a4131510.leadingMargin + 120>",
    "<NSLayoutConstraint:0x7fb9a4137e10 H:|-(0)-[GADBannerView:0x7fb9a2f128e0]   (Names: '|':UIView:0x7fb9a4131510 )>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x7fb9a4132e00 GADBannerView:0x7fb9a2f128e0.leading == UIView:0x7fb9a4131510.leadingMargin + 120>

Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful.
2015-10-27 15:16:01.147 Chat for WhatsApp[25348:2400768] Unable to simultaneously satisfy constraints.
    Probably at least one of the constraints in the following list is one you don't want. 
    Try this: 
        (1) look at each constraint and try to figure out which you don't expect; 
        (2) find the code that added the unwanted constraint or constraints and fix it. 
(
    "<NSLayoutConstraint:0x7fb9a4132db0 UIView:0x7fb9a4131510.trailingMargin == GADBannerView:0x7fb9a2f128e0.trailing + 120>",
    "<NSLayoutConstraint:0x7fb9a4137e60 H:[GADBannerView:0x7fb9a2f128e0]-(0)-|   (Names: '|':UIView:0x7fb9a4131510 )>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x7fb9a4132db0 UIView:0x7fb9a4131510.trailingMargin == GADBannerView:0x7fb9a2f128e0.trailing + 120>

Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful.
2015-10-27 15:16:01.172 Chat for WhatsApp[25348:2400768] Unable to simultaneously satisfy constraints.
    Probably at least one of the constraints in the following list is one you don't want. 
    Try this: 
        (1) look at each constraint and try to figure out which you don't expect; 
        (2) find the code that added the unwanted constraint or constraints and fix it. 
(
    "<_UILayoutSupportConstraint:0x7fb9a4129e50 V:[_UILayoutGuide:0x7fb9a41321c0(0)]>",
    "<_UILayoutSupportConstraint:0x7fb9a4129e00 _UILayoutGuide:0x7fb9a41321c0.bottom == UIView:0x7fb9a4131510.bottom>",
    "<NSLayoutConstraint:0x7fb9a4132e50 V:[GADBannerView:0x7fb9a2f128e0]-(0)-[_UILayoutGuide:0x7fb9a41321c0]>",
    "<NSLayoutConstraint:0x7fb9a41380d0 V:[GADBannerView:0x7fb9a2f128e0]-(1)-|   (Names: '|':UIView:0x7fb9a4131510 )>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x7fb9a4132e50 V:[GADBannerView:0x7fb9a2f128e0]-(0)-[_UILayoutGuide:0x7fb9a41321c0]>

Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful.
2015-10-27 15:16:01.180 Chat for WhatsApp[25348:2400768] Unable to simultaneously satisfy constraints.
    Probably at least one of the constraints in the following list is one you don't want. 
    Try this: 
        (1) look at each constraint and try to figure out which you don't expect; 
        (2) find the code that added the unwanted constraint or constraints and fix it. 
(
    "<NSLayoutConstraint:0x7fb9a2f07260 H:[GADBannerView:0x7fb9a2f128e0(320)]>",
    "<NSLayoutConstraint:0x7fb9a4137e10 H:|-(0)-[GADBannerView:0x7fb9a2f128e0]   (Names: '|':UIView:0x7fb9a4131510 )>",
    "<NSLayoutConstraint:0x7fb9a4137e60 H:[GADBannerView:0x7fb9a2f128e0]-(0)-|   (Names: '|':UIView:0x7fb9a4131510 )>",
    "<NSLayoutConstraint:0x7fb9a2c1edf0 'UIView-Encapsulated-Layout-Width' H:[UIView:0x7fb9a4131510(768)]>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x7fb9a2f07260 H:[GADBannerView:0x7fb9a2f128e0(320)]>

Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful.

这可能是一个约束问题?我有一个 WKWebView 和一个视图(横幅视图)

这是我的限制:

  let viewsDictionary = ["top":webView, "bottom":bannerView]

    let view_constraint_H:NSArray = NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[top]-0-|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: viewsDictionary)
    let view_constraint_H2:NSArray = NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[bottom]-0-|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: viewsDictionary)
    let view_constraint_V:NSArray = NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[top]-[bottom]-1-|", options: NSLayoutFormatOptions.AlignAllLeading, metrics: nil, views: viewsDictionary)

    view.addConstraints(view_constraint_H as! [NSLayoutConstraint])
    view.addConstraints(view_constraint_H2 as! [NSLayoutConstraint])
    view.addConstraints(view_constraint_V as! [NSLayoutConstraint])

提前我的横幅视图不是 320x50,因为我在 iPad 上使用它 请帮助我,非常感谢您提前

【问题讨论】:

  • 关闭和打开是什么意思?它会出现吗?
  • 当我关闭应用程序时,比如点击主页按钮然后我返回应用程序,广告就会隐藏
  • 尝试在重新打开应用时记录横幅视图,是否设置了约束?由于布局错误,它可能正在获取新广告或隐藏
  • @ogres 请查看编辑,谢谢
  • 试试这个link 有同样的问题,它有帮助..

标签: ios swift admob


【解决方案1】:

NSLayoutFormatOptions.AlignAllLeading 替换为NSLayoutFormatOptions(rawValue: 0),因为它会尝试添加不必要的约束。从情节提要中删除所有约束

首先,在 ViewController 中为广告高度声明新的约束,然后创建约束

let viewsDictionary = ["top":webView, "bottom":bannerView]

let view_constraint_H:NSArray = NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[top]-0-|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: viewsDictionary)
let view_constraint_H2:NSArray = NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[bottom]-0-|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: viewsDictionary)
let view_constraint_V:NSArray = NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[top]-[bottom]-1-|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: viewsDictionary)

view.addConstraints(view_constraint_H as! [NSLayoutConstraint])
view.addConstraints(view_constraint_H2 as! [NSLayoutConstraint])
view.addConstraints(view_constraint_V as! [NSLayoutConstraint])


constraintAdHeight = NSLayoutConstraint(item: bannerView, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: getAdsHeight())
view.addConstraint(constraintAdHeight)

为此,您将需要getAdsHeight() 方法,

func getAdsHeight() -> CGFloat {
    if(UIInterfaceOrientationIsPortrait(UIApplication.sharedApplication().statusBarOrientation)) {
        return CGSizeFromGADAdSize(kGADAdSizeSmartBannerPortrait).height
    } else {
        return CGSizeFromGADAdSize(kGADAdSizeSmartBannerLandscape).height
    }
}

并且每次布局/方向改变时,您都可以更新高度。

override func viewWillLayoutSubviews() {
    super.viewWillLayoutSubviews();
    self.constraintAdHeight.constant = getAdsHeight()
}

您也可以在隐藏广告时将constraint.constant设置为0(加载失败)

【讨论】:

  • 我的代码很快,无论如何我设置了约束但广告不再显示......
  • 好的,我删除了我在故事板中为横幅视图设置的约束,但现在广告不再位于底部,但现在即使我关闭也能正常工作。请问我该如何解决这个问题?
  • 谢谢,但它在这一行崩溃:view.addConstraint(constraintAdHeight)
  • 是的,因为我有一个占位符 UIView() 而不是bannerView,现在试试bannerView
  • 你复制了所有的功能吗?你覆盖 will layoutsubviews 吗?尝试记录广告视图并查看其框架/属性
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-01-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-01-13
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多