What width should I set UIBarButtonItem to create a square button?

I want to have a square button on my toolbar. Mine UIBarButtonItemuses an image wide enough to push my button into a rectangular shape. I looked through the docs but couldn't find the best size to use.

Looking at the other answers, 29.0it seems to be the overall size, but I would like confirmation. This is how I set my button up:

UIBarButtonItem *locationButtonItem = [[UIBarButtonItem alloc] 
                                   initWithImage:[UIImage imageNamed:@"location.png"]
                                   style:UIBarButtonItemStyleBordered
                                   target:self
                                   action:@selector(locationButtonTapped:)];
[locationButtonItem setWidth:29.0f];

What width should I set so that my toolbar button makes it square?

+3
source share
4 answers
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setBackgroundImage:[UIImage imageNamed:@"location.png"] forState:UIControlStateNormal];
    button.frame=CGRectMake(0,0, 29, 29);
[button addTarget:self action:@selector(locationButtonTapped:) forControlEvents:UIControlEventTouchUpInside];

UIBarButtonItem *btnDone = [[UIBarButtonItem alloc] initWithCustomView:button];
//[btnDone setTarget:self];

self.navigationItem.rightBarButtonItem = btnDone;
[btnDone release];

Try this ... maybe this will help you.

+10
source

UIKit Artwork Extractor , UIBarButtonImage - , - 13x16 @1x . "", UIBarButtonSystemItemAdd (UINavigationBarAddButton.png), . , -.

+1

Try the following: [locationButton setWidth:locationButton.height];

0
source

You cannot change the width of a UIBarButton.

Instead, if you need custom widths, you will need to use initWithCustomView.

You can use the width property only if the button is in a UIToolbar.

0
source

All Articles