Adding constraint to banner iAd

| | August 11, 2015

Working in XCode 6.4, Swift 1.2. In a Master-Detail setup.

I need position a banner ad (myAd) below the navigation bar for the Master screen (at the top of the screen).

I also have a fixed field on my screen, below the banner ad, as follows:

@IBOutlet weak var recordCount: UILabel!

I’ve also coded the iAd banner to show up at the top of the screen.

I need to have the banner add always show up below the Master navigation bar, and have UILabel! below the banner Ad. recordCount is then below the banner ad.

Once a user opts out of ads, the banner ad should be removed programatically, and recordcount would move up the screen vertically the same number of pixels equal to the height of the banner ad.

As it stands right now, UILabel! is hidden under the banner ad.

For this, I need to add the banner constraints programatically. The constraints need to

  • center the banner ad horizontally, edge to edge.
  • ensure the banner ad is immediately below the navigation bar
  • ensure that when the banner ad is removed programatically, that recordcount moves up the screen.

The code below loads the ad ok.

myAd = ADBannerView(adType: .Banner)
myAd.setTranslatesAutoresizingMaskIntoConstraints(false)
myAd.delegate = self
myAd.hidden = true
view.addSubview(myAd)

I’ve tried the following code-based constraints, but nothing is happening.

let viewsDictionary = ["bannerView": bannerView]
view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:[bannerView]-[recordCount]|", options: .allZeros, metrics: nil, views: viewsDictionary))

Question #1: Is adding “-[recordCount]” needed?

Question #2: What is the proper way of doing this?

Question #3: How does the “options” parameter work? I’ve been unable to find other references beyond the “.allZeros”. Can anyone point me to information related to what values are allowed in here?

Leave a Reply