Updating storyboard constraint with code

I have some code UIImage

that I would like to grow vertically on a button click. UIImage

is completely constrained in the storyboard and I would like to change its height when the button is clicked.

I linked UIImage

and its height limit in code:

    @IBOutlet weak var botBotCons: NSLayoutConstraint!
    @IBOutlet weak var botBot: UIImageView!

      

Then

@IBAction func testButton(sender: UIBarButtonItem) {
    println("testButton pressed")
    self.botBotCons.constant = 68   //or +=62 as its current constant is 6
    self.view.layoutIfNeeded()
}

      

I get the testButton message in the console, but the image doesn't change. What am I doing wrong? Do I need to change anything in the storyboard?

+1


source to share


1 answer


You have to call layoutIfNeeded

in the animation block. Apple actually recommends calling it once before the animation block to ensure that all pending layout operations complete. I just tested it with a button resize - everything works fine.

@IBOutlet weak var myBtn: UIButton!
@IBOutlet weak var btnHeight: NSLayoutConstraint!
@IBOutlet weak var btnWidth: NSLayoutConstraint!


@IBAction func resizeBtn(sender: AnyObject) {
    self.myBtn.titleEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 0)

    self.view.layoutIfNeeded()
    self.btnHeight.constant += 50
    self.btnWidth.constant += 50

    UIView.animateWithDuration(0.7, animations: {
        self.view.layoutIfNeeded()
    })
}

      



PS make sure no other restrictions are blocking your changes.

0


source







All Articles