Cole
Cole

Reputation: 2646

Button not changing width in animation

I have a button that, when I press it, I want to shrink in width. However, my current method (and the one I've seen everywhere online) doesn't seem to work.

Here's the method

@IBAction func addToOrderPressed(sender: AnyObject) {

        UIView.animateWithDuration(1.0, animations: {
            self.addToOrderButton.frame = CGRectMake(
                    self.addToOrderButton.frame.origin.x,
                    self.addToOrderButton.frame.origin.y,
                    self.addToOrderButton.frame.size.width - 100,
                    self.addToOrderButton.frame.size.height)
        })
        self.addProductToCart(productAtIndex)

        println("[Info] Product Added: \(productAtIndex.title)")
        println("[Info] Cart Count: \(self.dm.cartItems.count)")
    }

The button taps, and the other operations run, but the button stays the same size.

I am using constraints on this button, here's a screenshot showing which ones I'm using:

enter image description here

Could the issue be that the constraints are taking priority over the size change?

Upvotes: 0

Views: 690

Answers (1)

muhammedkasva
muhammedkasva

Reputation: 672

if you use autolayout for your button, you need to change constraint for width. Firstly, you can create IBOutlet NSLayoutConstraint for button width constraint.

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *widthConstraint;

[self.view setNeedsUpdateConstraints];
[self.view layoutIfNeeded];
UIView.animateWithDuration(1.0, animations: {
     widthConstraint = widthConstraint - 100
     [self.view layoutIfNeeded];
})

Upvotes: 2

Related Questions