1

If I add one label below another in interface builder, I get a suggestion of where to place it (which I guess has something to do with how far apart Apple thinks things should be spaced). If you don't know what I'm talking about, drag a new label to just below another label, and it should snap into place and a blue dotted line will appear.

Now I'm creating a view programatically and I want to get that same spacing. Is there a constant somewhere I can use?


EDIT: Did some more googling and found this is called an 'Aqua space' - which led me to this:

What constant can I use for the default Aqua space in Autolayout?

Community
  • 1
  • 1
Craig Brown
  • 1,891
  • 1
  • 24
  • 25
  • I had already looked there but couldn't find it. I did consider that, but I'd like to find a more elegant solution than working out what it should be and hard-coding the value. – Craig Brown Jan 06 '14 at 22:17

2 Answers2

0

When you create your view in code, you specify the frame with exact spaces or you use auto layout (which is a total pain in code).

The constants that Apple uses are generally 0, 8, and 20. These constants are generally based relative to other views.

Here's an example: Say you wanted to place a view, newView below another view, oldView with 8 point spacing.

UIView *oldView;
CGRect newViewFrame = oldView.frame;
newViewFrame.origin.y += oldView.frame.size.height + 8.0f;
UIView *newView = [[UIView alloc] initWithFrame:newViewFrame];
[self.view addSubview:newView];
Kevin
  • 16,696
  • 7
  • 51
  • 68
  • Thanks, it seems like this is the easier way to do things but autolayout feels like the 'more correct' way. If I get annoyed with autolayout though I'll just do this. – Craig Brown Jan 06 '14 at 22:29
0

Default spacing can be added in code using visual format language - the default spacing is represented by a - character. (This assumes you are using Autolayout, which you should be).

This will be different if it is spacing from a superview edge as opposed to spacing between two peer views, but it will match the defaults from interface builder.

I've written about visual format language, and the rest of Autolayout, here if you are interested.

jrturton
  • 118,105
  • 32
  • 252
  • 268
  • Thanks, this looks like exactly what I need. I hadn't been using autolayout, looks like it will make things easier (in the long run anyway, after I've learnt how to use it). Only problem I've found so far is that by using the | character to represent the superview, it's ignoring the top and bottom bars. Plus I'm trying to do this in a subclass of a UIScrollView, which doesn't seem to work the same as for a UIView. – Craig Brown Jan 06 '14 at 22:23
  • For layout guides, see my answer here: http://stackoverflow.com/a/20920178/852828 for scroll views, yes, these are harder. There are a couple of good guides around, and I'm working on my own... – jrturton Jan 07 '14 at 07:57