Site icon Ryadel

Objective C: removing UIStoryboard constraints programmatically

Objective C: rimuovere da codice i vincoli impostati tramite Storyboard

Since XCode made them available, Storyboards became a key concept for almost any GUI-oriented app: there's nothing wrong about it - there isn't a better way to design & arrange your views, objects and your whole UI in timely fashion. Expecially if you properly learn to use the Storyboard constraints, which are a powerful way to place your items around and ensure they'll stay in their place.

Nonetheless, there could be situations where you need to programmatically remove the Storyboard-defined constraints: for example, if you need to change an item position right after an user interaction.

In order to handle such scenarios I wrote the following static method:

You can throw it in any helper class you already have or create another one (mine is called LayoutHelper): once you did that, you can use it in the following way:

As you might see you can configure it to remove ancestor and/or children constraints. Needles to say, either self.myView and their parent / children can be items of any UIView derived class (UIButton, UIImage, UILabel, etc.).

Keep in mind that, once you removed the Storyboard-defined constraints, you can also programmatically add new ones using the method explained here.

Happy coding!

 

Exit mobile version