I'm trying to add an Admob Banner at the bottom of my UIViewController:
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())
}
I call this function in viewDidLoad
but app crashes with:
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'NSLayoutConstraint for >: Constraint items must each be a view or layout guide.'
I used an official example https://developers.google.com/admob/ios/banner
What are layout guides? Layout guides help you maintain the alignment of objects, such as pictures, text boxes, and tables. When enabled, the layout guides give you visible guidance to align objects on the page.
The topLayoutGuide property comes into play when a view controller is frontmost onscreen. It indicates the highest vertical extent for content that you don't want to appear behind a translucent or transparent UIKit bar (such as a status or navigation bar).
Most constraints define a relationship between two items in our user interface. These items can represent either views or layout guides. Constraints can also define the relationship between two different attributes of a single item, for example, setting an aspect ratio between an item's height and width.
The error is in the first constraint. The correct formulation in iOS 12 is:
NSLayoutConstraint(item: bannerView,
attribute: .bottom,
relatedBy: .equal,
toItem: view.safeAreaLayoutGuide,
attribute: .bottom,
multiplier: 1,
constant: 0)
It means that the attribute bottom
of the bannerView
has to be equal
to the bottom
attribute of the view.safeAreaLayoutGuide
.
Replace
toItem: view.safeAreaLayoutGuide.bottomAnchor,
With
toItem: view.safeAreaLayoutGuide,
Or
toItem: view,
You specify an anchor while as the exception says it must be a usual view or a layoutGuide
This worked:
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,
attribute: .bottomMargin,
multiplier: 1,
constant: 0),
NSLayoutConstraint(item: bannerView,
attribute: .centerX,
relatedBy: .equal,
toItem: view,
attribute: .centerX,
multiplier: 1,
constant: 0)
])
bannerView.load(GADRequest())
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With