Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to prevent UIBarButtonItem with custom attributes from changing size

I have a UIBarButtonItem in a UIToolbar at the top of my iPad app (iOS 5.1.) I have its width set to 65 in Interface Builder. It is of style 'bordered' and identifier 'custom.' The text label and tint changes when pressed:

    [btnA setTitle:@"State A"];
    [btnA setTintColor:[UIColor STATE_A_COL];

And so on, taking on various labels and colors. This worked fine, the button didn't resize even though the titles for the various states are quite different in length.

I then added this code to set the font, on startup:

UIFont * futura = [UIFont fontWithName:@"Futura" size:13];
NSDictionary *attributes = [NSDictionary dictionaryWithObject:futura
                                                       forKey:UITextAttributeFont];
[btnA setTitleTextAttributes:attributes forState:UIControlStateNormal];

Now, the button is sized to fit the width of the title it has on startup. It changes size as the titles change. How can I lock the size? I don't understand the interplay here; I thought all I'd done was change the title font attribute, not anything else about the button.

I have also tried explicitly setting the width property:

[btnA setWidth:65.0];

Again to no avail.

like image 502
Tim Avatar asked Oct 09 '12 18:10

Tim


1 Answers

I got the UIBarButtonItem to stop resizing by using the possibleTitles property, to give a hint as to the desired maximum width.

[btnA setPossibleTitles:[NSSet setWithObjects:@"State A', @"B", @"Final state", nil]];

This worked but I can't explicitly set the width to the size I want, so I'm leaving the question open.

like image 85
Tim Avatar answered Sep 20 '22 06:09

Tim