Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swap views but maintain constraints in Interface Builder

Tags:

In Interface Builder, I have a UIView with a complex set of constraints attached to it. What is the best way of exchanging this UIView for a UIImageView without having to recreate all the constraints?

Deleting the UIView deletes its constraints too.

NB: Simply setting the UIView's class to UIImageView won't work because it doesn't give me all the changeable attributes associated with the image view.

like image 266
HughHughTeotl Avatar asked Jul 13 '15 17:07

HughHughTeotl


People also ask

How do you add constraints in stack view?

fill distribution creates constraints attaching the top of the first arranged subview to the top of the stack view, and the bottom of the last arranged subview to the bottom of the stack view. It also creates a constraint between the top/bottom of adjacent subviews with the spacing of the stack view.

What are two properties that auto layout constraints control on a UIView?

Auto Layout defines margins for each view. These margins describe the preferred spacing between the edge of the view and its subviews. You can access the view's margins using either the layoutMargins or layoutMarginsGuide property.


1 Answers

Infuriatingly, there is no smart answer to this. The IB simply has no ability to let you switch control types. Oh, Xcode, you do let us down.

However there is a hacky way of doing it, described by this blog: http://codenetwaves.blogspot.com.br/2012/09/change-view-to-scrollview-in-xcode.html

The steps are:

  1. Right-click the storyboard, select Open As --> Source Code

  2. The storyboard is an XML file. Find the view you want to change. Replace <view ...> ... </view> with <imageView ...> ... </imageView>. Don't change anything else.

  3. Right-click the storyboard again, and open in Interface Builder. The views have been converted to UIImageViews and you can access all the normal UIImageView attributes.

like image 75
HughHughTeotl Avatar answered Sep 20 '22 15:09

HughHughTeotl