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!



About Ryan

IT Project Manager, Web Interface Architect and Lead Developer for many high-traffic web sites & services hosted in Italy and Europe. Since 2010 it's also a lead designer for many App and games for Android, iOS and Windows Phone mobile devices for a number of italian companies.

View all posts by Ryan