Search code examples
iosconstraintsnslayoutconstraint

iOS: updating constraints and frame at the same time


I have in my app one UITextField on the left and one UIButton on the right. The textfield is anchored on the left at the superview (a container view) and in the right to the button. So in the left of textfield there is a

leading space = 0 in relation of container

and on the right a

trailing space = 0 in relation of button

but if I move the button on the right way, changing the x origin value, why the textfield don't enlarge its width?

(obviously the button has its constraints about width and height and for position, but not that lock the textfield)

so if I do this

self.mybutton.frame = CGRectMake(self.mybutton.frame.origin.x+100, self.mybutton.frame.origin.y, self.mybutton.frame.size.width, self.mybutton.frame.size.height);

the button moved in the right direction but the textfield seems to doesn't enlarge its width,. Do you know why?


Solution

  • Could you try animating the button's trailing constraint?

    Like so (I changed the constraint inside an animation block for illustration purposes):

    UIView.animateWithDuration(
        5.0,
        animations: {
            self.buttonTrailingMarginConstraint.constant = 0
            self.view.layoutIfNeeded() // Necessary when changing constraints.
        }
    )
    

    Final result:

    Example

    Git clone project: https://github.com/backslash-f/animating-constraints