Change a constraints constant using code

| | August 18, 2015

I am creating this generic base code for all my views, It creates an ad bar that flows across all my pages. I have just included some code from the best answer to this Question and I cant work out why it doesn’t work. I am trying to make it so if the adBanner doesn’t load my labels stretch out and take up that space.
I apologise if this is obvious but I am new to this.
Here is my code

import Foundation
import UIKit
import iAd


class dayPicker: UIViewController  , ADBannerViewDelegate{

var UIiAd: ADBannerView = ADBannerView()
var SH = UIScreen.mainScreen().bounds.height
var AH = CGFloat()
@IBOutlet var constOne: NSLayoutConstraint!
@IBOutlet var constTwo: NSLayoutConstraint!

func appdelegate() -> AppDelegate {
    return UIApplication.sharedApplication().delegate as! AppDelegate
}

override func viewWillDisappear(animated: Bool) {
    UIiAd.delegate = nil
    UIiAd.removeFromSuperview()
}

func bannerViewDidLoadAd(banner: ADBannerView!) {
    UIView.beginAnimations(nil, context: nil)
    UIView.setAnimationDuration(1)
    UIiAd.alpha = 1
    AH = 50
    UIView.commitAnimations()
    self.constOne.constant == 58
    self.constTwo.constant == 58
}

func bannerView(banner: ADBannerView!, didFailToReceiveAdWithError error: NSError!) {
    UIView.beginAnimations(nil, context: nil)
    UIView.setAnimationDuration(1)
    UIiAd.alpha = 0
    AH = 0
    UIView.commitAnimations()
    self.constOne.constant == 8
    self.constTwo.constant == 8


}
override func viewWillAppear(animated: Bool) {

    UIiAd.delegate = self
    UIiAd = self.appdelegate().UIiAd
    UIiAd.frame = CGRectMake(0, SH - AH , 0, 0)
    self.view.addSubview(UIiAd)

}


}

One Response to “Change a constraints constant using code”

  1. You are giving == instead of = while assigning.

    import Foundation
    import UIKit
    import iAd
    
    class dayPicker: UIViewController  , ADBannerViewDelegate{
    
    var UIiAd: ADBannerView = ADBannerView()
    var SH = UIScreen.mainScreen().bounds.height
    var AH = CGFloat()
    @IBOutlet var constOne: NSLayoutConstraint!
    @IBOutlet var constTwo: NSLayoutConstraint!
    
    func appdelegate() -> AppDelegate {
        return UIApplication.sharedApplication().delegate as! AppDelegate
    }
    
    override func viewWillDisappear(animated: Bool) {
        UIiAd.delegate = nil
        UIiAd.removeFromSuperview()
    }
    
    func bannerViewDidLoadAd(banner: ADBannerView!) {
        UIView.beginAnimations(nil, context: nil)
        UIView.setAnimationDuration(1)
        UIiAd.alpha = 1
        AH = 50
        UIView.commitAnimations()
        self.constOne.constant = 58
        self.constTwo.constant = 58
    }
    
    func bannerView(banner: ADBannerView!, didFailToReceiveAdWithError error: NSError!) {
        UIView.beginAnimations(nil, context: nil)
        UIView.setAnimationDuration(1)
        UIiAd.alpha = 0
        AH = 0
        UIView.commitAnimations()
        self.constOne.constant = 8
        self.constTwo.constant = 8
    
    
    }
    override func viewWillAppear(animated: Bool) {
    
        UIiAd.delegate = self
        UIiAd = self.appdelegate().UIiAd
        UIiAd.frame = CGRectMake(0, SH - AH , 0, 0)
        self.view.addSubview(UIiAd)
    
    }
    
    
    }
    

Leave a Reply