当我重新打开应用程序时,Swift admob会隐藏

时间:2015-10-27 13:54:16

标签: ios swift admob

我有一个带有横幅视图的视图控制器,我整合了一个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和一个View(横幅视图)

这是我的约束:

  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 请帮帮我,非常感谢你提前

1 个答案:

答案 0 :(得分:1)

NSLayoutFormatOptions.AlignAllLeading替换为NSLayoutFormatOptions(rawValue: 0),因为它会尝试添加非必要的约束。从Storyboard

中删除所有约束

首先,在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