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. Microsoft MVP for Development Technologies since 2018.

View all posts by Ryan

2 Comments on “Objective C: removing UIStoryboard constraints programmatically”

  1. Pingback: Objective C: programmatically align an UIView object to its parent - Ryadel
  2. Pingback: Remove all constraints affecting a UIView - DexPage

Leave a Reply

Your email address will not be published. Required fields are marked *

The reCAPTCHA verification period has expired. Please reload the page.

This site uses Akismet to reduce spam. Learn how your comment data is processed.