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?
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 aUIViewsubclass. You don’t need all theUIBarButtonItemfeatures in your case…. just the background I presume.Then setting
UILabel.frame(with the margins you want) +addSubview:+ aUITextAlignmentCentershould do the job! Plus maybe also a autoresizingMask withUIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeightif you have manage device Orientation…EDIT 1 :
Since there are some doubts about that proposal, I made a quick test :
Here is the result :
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 :
brings that :
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
UIBarButtonSystemItemFlexibleSpaceare for after all…) :The result :
EDIT 4 :
As a side not, after working more on a
UIToolbar,12pxis 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 -10pxUIBarButtonSystemItemFixedSpace… Handy!