Aryan Sharma
Aryan Sharma

Reputation: 635

swift update constraints programmatically

I have a UIView in which there are 4 components, UITextField, a UIActivityIndicator and 2 UIButtons. All the components are using constraints set using storyboard.

Button1 UITextField UIActivityIndicator Button2

The UITextField displays the url of a UIWebView and the UIActivityIndicator corresponds to page loading.

When the page has loaded, UIActivityIndicator is hidden.

What I want is that when the loading is complete and UIActivityIndicator is hidden, the size of UITextField increases and use up the space used by the UIActivityIndicator.

Button1 UITextField Button2

Upvotes: 0

Views: 1215

Answers (1)

Duncan C
Duncan C

Reputation: 131511

Here's what I would do:

Rather than putting a constraint between the text field and the activity indicator, use a constraint between the text field and it's superview, with extra padding added to make room for the activity indicator.

Control-drag from the constraint into your view controller to create an outlet to it.

In your code, when you hide the activity indicator, reduce the constant setting on the constraint by the width of the activity indicator plus the space between the activity indicator and the text field so that the text field gets wider to fill the space that's freed by the hiding of the activity indicator.

If you want the text field to animate it's growth, put a call to layoutIfNeeded() in a call to UIView.animate(duration:animations:) immediately after the code that changes the constraint setting. That will cause the layout change to be animated.

Upvotes: 1

Related Questions