Hari Honor
Hari Honor

Reputation: 8914

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?

Upvotes: 30

Views: 3148

Answers (4)

AnthoPak
AnthoPak

Reputation: 4391

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…

Upvotes: 6

Andrew Redko
Andrew Redko

Reputation: 21

As for now, it is not possible to do it all right. But here is what worked best for me on Xcode 9. Note that this way you will loose constraints of the top view that you are embedding. So, to embed one view into another:

  1. Create another blank view on the same level as a view you are going to embed.
  2. Position the new view properly and add constraints from it to the superview. I think in most cases you want to duplicate the same constraints as you have from existing view to the superview.
  3. Then, select the view that needs to be embedded and drag it under the new view. Now you have your view embedded into the new view. What you also need is to add 0 edge constraints from embedded view to the new view.

That's it.

Upvotes: 0

Brian Ogden
Brian Ogden

Reputation: 19212

All you have to do, at least in Xcode 7.2 is give the Container View the following constraints

Trailing Space to Superview = -20

Leading Space to Superview = -20

Upvotes: 0

Zack Morris
Zack Morris

Reputation: 4823

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

Upvotes: 37

Related Questions