Andrea Venanzi
Andrea Venanzi

Reputation: 250

Autolayout differs from iOS 7 and iOS 8

I'm having an issue between an iAd BannerView, created in IB, and its constraints. I have an IBOutlet for bottom constraint of an iAd BannerView with superview. In viewDidLoad() of ViewController I set the outlet to 0 minus banner height to put iAd BannerView outside the bottom screen margin.

@IBOutlet var adBannerView: ADBannerView!
@IBOutlet var adBannerBottomConstraints: NSLayoutConstraint!

 override func viewDidLoad() {
    super.viewDidLoad()

    // Configure answers index
    self.arrayAnswIndex = ["A", "B", "C", "D", "E"]

    // TableView Cell
    var nib = UINib(nibName: "QuestionTableViewCell", bundle: nil)
    self.simulazioneTableView.registerNib(nib, forCellReuseIdentifier: self.QuestionCellIdentifier)
    var nibAnswer = UINib(nibName: "AnswerTableViewCell", bundle: nil)
    self.simulazioneTableView.registerNib(nibAnswer, forCellReuseIdentifier: self.AnswerCellIdentifier)

    // NavigationBar
    self.navigationBarSettings()

    // TabBar
    self.tabBarController?.tabBar.hidden = true

    // iAd Banner
    adBannerView.hidden = true
    adBannerBottomConstraints.constant = 0 - self.adBannerView.bounds.size.height        
}

When ad is loaded I animate constraint to show the banner with:

func bannerViewDidLoadAd(banner: ADBannerView!) {
    if (adBannerView.hidden == true) {
        //now show banner
        adBannerView.hidden = false
        self.adBannerBottomConstraints.constant = self.adBannerBottomConstraints.constant + self.adBannerView.frame.size.height
        UIView.animateWithDuration(0.4, animations: {
            self.view.layoutIfNeeded()
        })
    }
}

The problem is that in iOS 8 everything works fine, in iOS 7 instead iAd BannerView is twice its size under bottom margin after viewDidLoad() and so when ad is loaded the banner remains outside the screen. I have temporarily resolved the issue checking the device version and modifying the constraint accordingly in viewDidLoad().

// iAd Banner
adBannerView.hidden = true
if ((UIDevice.currentDevice().systemVersion as NSString).floatValue >= 8.0) {
     adBannerBottomConstraints.constant = 0 - self.adBannerView.bounds.size.height
} else {
     adBannerBottomConstraints.constant = 0
}

There is a better way to accomplish my purpose? Thank you guys!

Andrea

Images:

Upvotes: 3

Views: 187

Answers (1)

matt
matt

Reputation: 534893

adBannerBottomConstraints.constant = 
    0 - self.adBannerView.bounds.size.height        

But that's your whole problem right there. This is exactly the sort of hard-coded arithmetic calculation based on assumptions about the actual values of things that constraints mean you should not be doing. The whole point of autolayout is that you do not calculate anything: you set constraints that describe where the view should be. If you want this thing to be below the bottom of the superview, pin its top to the bottom of the superview!

Then, when you want to show it, delete that constraint and pin its bottom wherever it needs to go so that it appears at the bottom.

Upvotes: 2

Related Questions