Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 767
skålfyfan Avatar asked Dec 22 '22 15:12

skålfyfan


2 Answers

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!

like image 91
Vincent Guerci Avatar answered Dec 24 '22 05:12

Vincent Guerci


There doesn't appear to be anywhere this margin is stored, but here's a way of calculating this in Xamarin. First add a button to the Toolbar in your ViewController:

UIButton button = new UIButton();
button.Tag = 999;
ToolbarItems = new[] { FilterButton };

Then use this code to read out (using a breakpoint) the value of the padding for any particular orientation and device. (I've included a variable for the orientation as this will tell you what the orientation of the app currently is if you're calling this code during a rotation event.)

if (NavigationController != null)
{
    UIButton firstButton = NavigationController.Toolbar.Subviews
        .FirstOrDefault(x => x.GetType() == typeof(UIButton) && x.Tag == 999);
    if (firstButton != null)
    {
        var orientation = UIApplication.SharedApplication.StatusBarOrientation;
        var padding = firstButton.Frame.X;
    }
}

For the record, for iOS9, the values are 16 for portrait and 20 for landscape on iPhones, and 20 for both on iPads.

like image 43
jimmyjudas Avatar answered Dec 24 '22 05:12

jimmyjudas