我正在尝试在 UIViewController 底部添加一个 Admob 横幅:
func addBannerViewToView() {
bannerView = GADBannerView(adSize: kGADAdSizeBanner)
bannerView.adUnitID = "ca-app-pub-HIDDEN/HIDDEN"
bannerView.rootViewController = self
bannerView.delegate = self
bannerView.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(bannerView)
view.addConstraints(
[NSLayoutConstraint(item: bannerView,
attribute: .bottom,
relatedBy: .equal,
toItem: view.safeAreaLayoutGuide.bottomAnchor,
attribute: .top,
multiplier: 1,
constant: 0),
NSLayoutConstraint(item: bannerView,
attribute: .centerX,
relatedBy: .equal,
toItem: view,
attribute: .centerX,
multiplier: 1,
constant: 0)
])
bannerView.load(GADRequest())
}
Run Code Online (Sandbox Code Playgroud)
我调用了这个函数,viewDidLoad但应用程序崩溃了:
由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“NSLayoutConstraint for >:每个约束项都必须是视图或布局指南。”
小智 17
错误在第一个约束中。iOS 12 中的正确公式是:
NSLayoutConstraint(item: bannerView,
attribute: .bottom,
relatedBy: .equal,
toItem: view.safeAreaLayoutGuide,
attribute: .bottom,
multiplier: 1,
constant: 0)
Run Code Online (Sandbox Code Playgroud)
这意味着属性bottom的bannerView必须是equal对bottom的属性view.safeAreaLayoutGuide。
| 归档时间: |
|
| 查看次数: |
3253 次 |
| 最近记录: |