30

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?

4 Answers 4

37

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

5
  • 6
    Nice technique. Thanks. But basically the correct answer is "You can't"?
    – Hari Honor
    Commented Jun 1, 2013 at 14:31
  • 1
    Worked in Xcode 5.1. This should be the correct answer because it provides a solution to the problem, even though it isn't as direct as desired. Commented Apr 29, 2014 at 2:47
  • Fantastic:) I like this.
    – Vignesh
    Commented Apr 30, 2014 at 10:12
  • 1
    Anyway, even if this answer is valid... Apple should solve this stupid issue.
    – MatterGoal
    Commented Feb 16, 2016 at 10:19
  • 1
    This no longer works in XCode 8.0. All the child view do go to their original positions, but all constraints are removed.
    – Alex311
    Commented Nov 23, 2016 at 22:43
6

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…

0

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
0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.