I am setting up a UINavigationBar with 2 buttons that change their title when pressed. The problem that I am having is if I use setTitle:@"IT"
and then use setTile:@"THEM"
the button changes size.
No problem I thought, I will make two images both 105x23 pixels in photoshop and add the text there. This however does not work as the buttons still seem to change size, maybe the button size is based on the none transparent pixels.
& both 105x23pxNOTE: the gray background is transparent, I mad it gray here to show the size of each
// BUTTON CHANGES SIZE
[self setNavBarItem:[[navBar items] objectAtIndex:0]];
[self setButtonStart:[navBarItem leftBa开发者_如何学运维rButtonItem]];
[self setButtonReset:[navBarItem rightBarButtonItem]];
[[self buttonStart] setImage:[UIImage imageNamed:@"IT.png"]];
[[self buttonReset] setImage:[UIImage imageNamed:@"THEM.png"]];
The solution I came up with was to set the button titles to a constant text spacer so that each time I change the image the button remains the same size as the button title always remains the same. It works fine, but I just wanted to heck if anyone else had come across this and maybe found an alternate solution?
// BUTTON MAINTAINS FIXED SIZE
[self setNavBarItem:[[navBar items] objectAtIndex:0]];
[self setButtonStart:[navBarItem leftBarButtonItem]];
[self setButtonReset:[navBarItem rightBarButtonItem]];
[[self buttonStart] setTitle:@"_______"];
[[self buttonReset] setTitle:@"_______"];
[[self buttonStart] setImage:[UIImage imageNamed:@"IT.png"]];
[[self buttonReset] setImage:[UIImage imageNamed:@"THEM.png"]];
set the possibleTitles property of the UIBarButtonItem. It should be exactly what you want.
possibleTitles The set of possible titles to display on the bar button.
@property(nonatomic, copy) NSSet *possibleTitles
Discussion
Use this property to provide a hint to the system on how to correctly size the bar button item to be wide enough to accommodate your widest title. Set the value of this property to an NSSet object containing all the titles you intend as possible titles for the bar button item. Use the actual text strings you intend to display.This property applies to bar button items placed on navigation bars or toolbars.
精彩评论