Search code examples
iphonecocoa-touchios4uitoolbar

UIToolbar - margins? Does anyone know the left and right margins for a UIToolbar?


I'm creating a UIToolbar with nothing but a UILabel placed on it. the label content is dynamic and can change and I'd like to center it on a UIToolbar.

Does anyone know the exact margin size for the left and right sides of a UIToolbar?

I'm creating a [[UIBarButtonItem alloc] initWithCustomView:myLabelContainer] and I'm trying to ensure that myLabelContainer takes up the entire toolbar space, minus the forced margins.

Right now I've guessed that the margins are about 12 px on each side so a UIView with a frame width of 296 should fill the entire UIToolbar?

Is this information available anywhere?


Solution

  • I don't think this information is available. But it should be pretty easy to get with some testing.

    If what you need is just a UILabel over the whole toolbar, I would suggest just to add it to the UIToolbar, which is a UIView subclass. You don't need all the UIBarButtonItem features in your case.... just the background I presume.

    Then setting UILabel.frame (with the margins you want) + addSubview: + a UITextAlignmentCenter should do the job! Plus maybe also a autoresizingMask with UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight if you have manage device Orientation...

    EDIT 1 :

    Since there are some doubts about that proposal, I made a quick test :

    UILabel *label = [[[UILabel alloc] initWithFrame:CGRectMake(10, 0, 300, 44)] autorelease];
    label.text = @"Hello world";
    label.textAlignment = UITextAlignmentCenter;
    label.textColor = [UIColor whiteColor];
    label.shadowColor = [UIColor blackColor];
    label.backgroundColor = RGBACOLOR(255, 0, 0, .3f);
    [self.toolbar addSubview:label];
    

    Here is the result :

    enter image description here

    EDIT 2 :

    Now that I've launced Xcode and wrote some code, that's easy to figure your primary question. Altering a bit the previous code :

    UILabel *label = [[[UILabel alloc] initWithFrame:CGRectMake(0, 0, 320, 44)] autorelease];
    ... same
    [self.toolbar setItems:
     [NSArray arrayWithObject:
      [[[UIBarButtonItem alloc] initWithCustomView:label] autorelease]]];
    

    brings that :

    toolbar2

    So as you guessed, there is a left margin of 12px, but Custom views doesn't look like to be resized to fit, therefore, no right margin in that case... unless you resize your view accordingly.

    EDIT 3 :

    Unless your UILabel needs a background, here is what I would probably do (That's what UIBarButtonSystemItemFlexibleSpace are for after all...) :

    UILabel *label = [[[UILabel alloc] init] autorelease];
    label.text = @"Hello world";
    ... same
    [label sizeToFit];
    [self.toolbar setItems:
     [NSArray arrayWithObjects:
      [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace
                                                     target:nil action:nil] autorelease],
      [[[UIBarButtonItem alloc] initWithCustomView:label] autorelease],
      [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace
                                                     target:nil action:nil] autorelease],
      nil]];
    

    The result :

    enter image description here

    EDIT 4 :

    As a side not, after working more on a UIToolbar, 12px is the default space added before a button. And I've just discovered a fun one, spacers are working with negative values!. I.E. if you want a 2px space between two buttons, just add a -10px UIBarButtonSystemItemFixedSpace... Handy!