Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to prevent Interface Builder from adding 20pt padding around subview content?

IB, stop adding that padding!

See that 20px (pt's rather) of padding? IB does it automatically in two cases, when you hit Cmd + = to autosize the superview to fit subview contents, and when you choose Editor > Embed In > UIView, UIScrollView etc..

The later is especially annoying as it takes what should be a one step time saver and turns it into a repositioning hassle that is only marginally better than doing it manually and losing the relative positioning of ALL the subviews when you drag them into a different place in the hierarchy.

Also with the Embed In option, IB shifts the positioning of the new superview wrapper by (-20, -20) as if that makes it better...

Am I missing something here? Is there a way to prevent this padding?

like image 381
Hari Honor Avatar asked Mar 13 '13 12:03

Hari Honor


2 Answers

I finally figured out how to do it, related to this answer:

Group views in Interface Builder

  1. Select all of the views that you wish to embed (by shift or command clicking them).
  2. Editor->Embed In->View. This creates a view "A" with the superfluous 20 pixel padding.
  3. Create a new view "B" with the actual bounds you desire (so 20 pixels smaller than "A" on all sides).
  4. Drag the old container view "A" into the new container view "B" (Xcode will center it for you within "B", preserving the current locations of all the child elements).
  5. Click on the old container view "A" and go to Editor->Unembed.

Now your child elements are correctly positioned within view "B", and there is no 20 pixel padding.

Tested in Xcode 4.5.

P.S. this technique can be especially useful when you wish to have a sidebar and support both 3.5" and 4" displays. You can give the sidebar and main view the appropriate autosizing and then scale subviews in relation to them. I was having trouble getting GLKit views to respect autosizing rules, so I set up ordinary views and embedded the GLKKit views within them with all of the red autosizing bars enabled:

GLKView nested subview frame size and bounds size incorrect

like image 148
Zack Morris Avatar answered Sep 22 '22 02:09

Zack Morris


What a great news ! This is finally possible to embed views in UIView without any tweaking thanks to Xcode 10 !

Just use the new View Without Insets submenu.

As of now, they didn't do the same for UIScrollView yet…

like image 36
AnthoPak Avatar answered Sep 20 '22 02:09

AnthoPak