Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Constraint items must each be a view or layout guide

Tags:

ios

admob

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

like image 965
user924 Avatar asked May 14 '19 20:05

user924


People also ask

What is a layout guide?

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.

What is top layout guide?

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).

What are constraints in IOS?

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.


3 Answers

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.

like image 104
Domingo Gallardo Avatar answered Oct 22 '22 17:10

Domingo Gallardo


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

like image 40
Sh_Khan Avatar answered Oct 22 '22 17:10

Sh_Khan


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())
    }
like image 38
Salman Khalid Avatar answered Oct 22 '22 15:10

Salman Khalid