1

I have a navigation controller in my app. I am adding UIBarButtonItem in the navigation bar of a view. The default width and height of UIBarButtonItem when I added them into the nav bar is 44*30. I want a UIBarButtonItem of size 30*30 in the navigation bar. How can I achieve this? I have tried changing the width property of UIBarbutton, but while exploring I come to know that it is possible only when we insert UIBarButtons in the ToolBar to change the default width and not possible when we insert into the navigation bar. Is there any way through which I can achieve this?

My basic need is that I want two square (30*30) bar buttons at the left of the nav bar and one on right of the nav bar. Yes I can insert simple button there and set there frames instead of bar buttons. But I just want to know is it possible to change the width of bar buttons.If yes,What is the way?

Anup H
  • 549
  • 7
  • 10

2 Answers2

1

Create One UIButton according your want and assign it as left and right bar button.You can change frame as per your want.In the same way you can do for right button.

UIButton *mybutton = [UIButton buttonWithType: UIButtonTypeCustom];
[mybutton setImage:[UIImage imageNamed:@"anyimage.png"]];
  [mybutton setFrame:CGRectMake(0,0,30,30)];
[mybutton addTarget:self action:@selector(onclick:) forControlEvents: UIControlEventTouchUpInside];
UIBarButtonItem *customItem = [[UIBarButtonItem alloc] initWithCustomView: mybutton];
[self.navigationItem setLeftBarButtonItem:customItem];

Updated:-Please use this as per your Border style.

 [self.navigationItem setRightBarButtonItem:[[UIBarButtonItem alloc]initWithTitle:@"Test" style:UIBarButtonItemStyleBordered target:self action:@selector(test:)]];
Sanoj Kashyap
  • 5,020
  • 4
  • 49
  • 75
  • Thank you very much Sandy! I tried the same solution yesterday but was not able get the output. I was doing something different. The solution that you have provided creates plain style button and not of the other styles provided by the apple . I tried setting the button style of the bar button but was not successful. Do you think using this method we can only create plain buttons and not the styled one? – Anup H Aug 21 '12 at 09:01
  • Can you tell which style button you want? – Sanoj Kashyap Aug 21 '12 at 09:28
  • I want bordered style buttons – Anup H Aug 21 '12 at 11:51
  • Sandy that is OK. But My question is How would you set the frame of the bar button in your updated version? I haven't got any success in achieving that. I have tried everything.I think through my experience that you have to sacrifice for one thing. If you go for custom frame then you won't be able to set any style to it and if you go for style then you won't be able to customize its frame. What do you think? – Anup H Aug 21 '12 at 14:48
  • We can not use the frame property for UIBarButton,Please see [link](http://stackoverflow.com/questions/2994354/figure-out-uibarbuttonitem-frame-in-window) OR you need to create the image with that bordered style that you want and use before updated code above.I don't find any other solution.If you find other that this .Please let me know. – Sanoj Kashyap Aug 22 '12 at 04:31
  • Sandy, actually we can not set its frame but if you see we can set its width property. Surprisingly the width can be set only when it is inserted into the toolbar but not when you insert that bar button in the navigation bar. I will definitely let you know if I find some solution. Thank you for your time. – Anup H Aug 22 '12 at 06:52
0
UIButton *desiredBtn = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 30, 30)];
UIBarButtonItem *aBarButtonItem = [[UIBarButtonItem alloc]  initWithCustomView:desiredBtn];

then add this barbutton to navigationBar

self.navigationItem.rightBarButtonItem = aBarButtonItem;
Deepak
  • 348
  • 4
  • 14