Question

I have been trying to get around setting navigationitem button's width property but couldn't find a way to get it done what exactly I am looking for.

Following code works fine setting button size what I want and appear nicely but I don't see actually button, its rather only my image. It should appear like button and it has background image!

UIButton *addCommentButton = [UIButton buttonWithType:UIButtonTypeCustom];
[addCommentButton setFrame:CGRectMake(0, 0, 25, 25)];
[addCommentButton addTarget:self action:@selector(addComment) forControlEvents:UIControlEventTouchUpInside];
[addCommentButton setBackgroundImage:[UIImage imageNamed:@"myImage.png"] forState:UIControlStateNormal];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:addCommentButton];

Following code does that, but I can't set button size (25,25)!!

self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"myImage.png"] style:UIBarButtonItemStyleBordered target:self action:@selector(addComment)];

I tried setting width explicitly but it does make no difference!

[[self.navigationItem rightBarButtonItem] setWidth] = 25;

Can someone please help me to sort out this issue. Thanks.

Was it helpful?

Solution

I don't have a quick explanation to fix your issue, but I think the code posted in the following link will help you solve it.

Customization of UINavigationBar and the back button

OTHER TIPS

Your first code snippet generates a custom button, which won't have a "button" look and feel. The second code snippet gives a UIBarButtonItem with a bordered UIBarButtonItem style (not a UIButton). Using the first code snippet, just create a UIButton with a button type RoundedRect instead of Custom.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top