0

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.

Paresh Masani
  • 7,474
  • 12
  • 73
  • 139

2 Answers2

0

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

Community
  • 1
  • 1
Wolfgang Schreurs
  • 11,779
  • 7
  • 51
  • 92
  • Thanks. That exact code doesn't work for me but I got an idea how to get my things done and implemented! I really surprised by the amount of code I need to integrate to just get a round button with image!! or just resizing the rightbarbutton item! – Paresh Masani Feb 07 '12 at 11:26
0

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.

jmstone617
  • 5,707
  • 2
  • 24
  • 26
  • I tried roundedrect as well. It doesn't allow me to specify background color! I want to create the same button in my second code snippet but want to resize it! – Paresh Masani Feb 07 '12 at 09:29