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

I am creating a UIToolbar that hosts only UILabel. the contents of the shortcuts is dynamic and can change, and I would like to focus it on the UIToolbar.

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

I am creating [[UIBarButtonItem alloc] initWithCustomView: myLabelContainer], and I am trying to ensure that myLabelContainer takes up all the space on the toolbar, minus the force fields.

Right now I guessed that the margins have about 12 pixels on each side, so should a UIView with a frame width of 296 have to fill the whole UIToolbar?

Is this information available anywhere?

+4
source share
2 answers

I do not think this information is available. But with some testing it should be pretty easy.

If you only need UILabel in the entire toolbar, I would suggest just adding it to the UIToolbar , which is a subclass of UIView . You do not need all the functions of UIBarButtonItem in your case .... only the background that I assume.

Then set UILabel.frame (with the fields you want) + addSubview: + a UITextAlignmentCenter should do the job! Plus, perhaps also autoresizingMask with UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight if you control device orientation ...

EDIT 1 :

Since there are some doubts about this suggestion, I did 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 have run Xcode and written the code, it is easy to understand your main question. By slightly changing 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 the following:

toolbar2

So, you guessed it, there is a 12px left edge, but custom views are not like their size being resized to fit, so in this case there is no right edge ... unless you resize your view accordingly.

EDIT 3 :

If your UILabel doesn't need a background, here's what I will probably do (which is for UIBarButtonSystemItemFlexibleSpace for everyone ...):

 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]]; 

Result:

enter image description here

EDIT 4 :

As a side, no, after working more on UIToolbar , 12px is the default space added before the button was clicked. And I just found it funny, delimiters work with negative values! . I.E. if you need 2px space between two buttons just add -10px UIBarButtonSystemItemFixedSpace ... Handy!

+9
source

It seems that this stock is not stored anywhere, but here is a way to calculate this in Xamarin. First add a button to the toolbar in the ViewController:

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

Then use this code to read (using a breakpoint) the fill value for any particular orientation and device. (I have included a variable for orientation, as this will tell you what the orientation of the application is currently if you call 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 iOS9, the values ​​are 16 for portrait and 20 for landscape on iPhone and 20 for both on iPad.

0
source

All Articles