adding button to navigationBar - ios

I want to have 2 right button in navigation bar. Is it possible to do and is it legitimate to app Store?

Yes, It can be..
self.navigationItem.rightBarButtonItems = arrayOfBarButtonItems;

UIButton *backButton = [[UIButton alloc] initWithFrame: CGRectMake(5.0, 25.0, 60.0, 32.0)];
[backButton setBackgroundImage:[UIImage imageNamed:#"back_btn.png"] forState:UIControlStateNormal];
[backButton.titleLabel setFont:[UIFont fontWithName:#"Helvetica-Bold" size:14.0]];
[backButton setTitle:#"back" forState:UIControlStateNormal];
[backButton addTarget:self action:#selector(back:) forControlEvents:UIControlEventTouchUpInside];
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
try this

Related

RightBar Button on navigation bar is not giving click like look

I want to show image and the title on RightBar Button so i have written code as below:
UIBarButtonItem *negativeSpacer = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil] autorelease];
negativeSpacer.width = BARBUTTONITEM_PADDING_IPAD;
UIImage *buttonImage = [UIImage imageNamed:#"select-ipad#3x.png"];
UIButton *selectButton = [UIButton buttonWithType:UIButtonTypeCustom];
[selectButton setImage:buttonImage forState:UIControlStateNormal];
[selectButton setTitle:#"Select" forState:UIControlStateNormal];
[selectButton setTitleColor:[UIColor colorWithRed:0.76 green:0.21 blue:0.08 alpha:1.0] forState:UIControlStateNormal];
[selectButton.titleLabel setFont:[UIFont fontWithName:#"HelveticaNeue" size:14.0]];
[selectButton sizeToFit];
UIBarButtonItem *aBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:selectButton];
[selectButton addTarget:self action:#selector(didSelectButton:) forControlEvents:UIControlEventTouchUpInside];
[self.navigationItem setRightBarButtonItems:[NSArray arrayWithObjects:negativeSpacer,aBarButtonItem,nil]];
This code shows the title and image on the NavigationBar and when i click on it, then the clicked like appears only on the image but not on the text.
-(void)viewWillAppear:(BOOL)animated{
UIButton *btn=[[UIButton alloc] initWithFrame:CGRectMake(0, 0, 25, 25)];
[btn setImage:[UIImage imageNamed:#"select-ipad#3x.png"] forState:UIControlStateNormal];
[btn addTarget:self action:#selector(onBtnRightClick:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *btnRight=[[UIBarButtonItem alloc]initWithCustomView:btn];
self.navigationItem.leftBarButtonItem = btnRight;
}
-(IBAction)onBtnRightClick:(id)sender
{
NSLog(#"Right Bar Button Clicked.");
}
Using your code, try 2 buttons:
UIButton *button1=[UIButton buttonWithType:UIButtonTypeCustom];
[button1 setFrame:CGRectMake(0.0, 0.0, 45.0, 45.0)];
[button1 addTarget:self action:#selector(didSelectButton:) forControlEvents:UIControlEventTouchUpInside];
[button1 setImage:[UIImage imageNamed:#"select-ipad#3x.png"] forState:UIControlStateNormal];
UIBarButtonItem *buttonItem1 = [[UIBarButtonItem alloc]initWithCustomView:button1];
UIButton *button2 = [UIButton buttonWithType:UIButtonTypeCustom];
[button2 addTarget:self action:#selector(didSelectButton:) forControlEvents:UIControlEventTouchUpInside];
[button2 setTitle:#"Select" forState:UIControlStateNormal];
[button2 setTitleColor:[UIColor colorWithRed:0.76 green:0.21 blue:0.08 alpha:1.0] forState:UIControlStateNormal];
[button2 setTitleColor:[UIColor blackColor] forState:UIControlStateHighlighted];
[button2.titleLabel setFont:[UIFont fontWithName:#"HelveticaNeue" size:14.0]];
[button2 sizeToFit];
UIBarButtonItem *buttonItem2 = [[UIBarButtonItem alloc] initWithCustomView:button2];
self.navigationItem.rightBarButtonItems = #[buttonItem2,buttonItem1];
I hope this can help you.

Adding buttons to UIToolBar Programmatically

I have added a toolbar and then added 3 buttons to it programmatically. However, the 3 buttons are all cluttered to a corner. What i want to do is to have even spaces between the 3 buttons so the UI look decent.
How can I do this?
UIButton *button = [UIButton buttonWithType:100];
[button addTarget:self action:#selector(hideGender:) forControlEvents:UIControlEventTouchUpInside];
[button setImage:[UIImage imageNamed:#"img"] forState:UIControlStateNormal];
[button setTitle:#"Images" forState:UIControlStateNormal];
button.imageEdgeInsets= UIEdgeInsetsMake(0.0, 0.0, 0, 50);
button.tag=0;
UIButton *button2 = [UIButton buttonWithType:100];
[button2 addTarget:self action:#selector(hideSchool:) forControlEvents:UIControlEventTouchUpInside];
button2.tag=2;
button2.imageEdgeInsets= UIEdgeInsetsMake(0.0, 0.0, 0, 50);
[button2 setImage:[UIImage imageNamed:#"img22"] forState:UIControlStateNormal];
UIButton *button3 = [UIButton buttonWithType:100];
[button3 addTarget:self action:#selector(hideCar:) forControlEvents:UIControlEventTouchUpInside];
button.tag=3;
button3.imageEdgeInsets= UIEdgeInsetsMake(0.0, 0.0, 0,50);
[button3 setImage:[UIImage imageNamed:#"img3"] forState:UIControlStateNormal];
UIBarButtonItem *barButtonItem0 = [[UIBarButtonItem alloc] initWithCustomView:button] ;
UIBarButtonItem *barButtonItem2 = [[UIBarButtonItem alloc] initWithCustomView:button2] ;
UIBarButtonItem *barButtonItem3 = [[UIBarButtonItem alloc] initWithCustomView:button3] ;
UPDATE: I want to use only UIbutton
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
[btn setBackgroundImage:[UIImage imageNamed:#"image.png"] forState:UIControlStateNormal];
[btn addTarget:self action:#selector(action:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *barBackButton = [[UIBarButtonItem alloc] initWithCustomView:btn];
[toolBar setItems:[NSArray arrayWithObject:barBackButton]];
Now in setItems set the array of buttons. Suppose you want to put three buttons, then width of each button will be screenWidth/3 and text will be center-aligned. In this way, there is no over-riding of button.

UIBarButtonItem with image doesn't show

I'm trying to set a UIBarButtonItem with an image. The problem is that it never appears. When I just add a UIBarButtonItem with a title it works.
UIBarButtonItem *openLeftSliderButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"filter.png"] style:UIBarButtonItemStylePlain target:self action:#selector(openLeftSlider)];
self.navigationItem.leftBarButtonItem = openLeftSliderButton;
UIImage *sideImage = [UIImage imageNamed:#"i1.jpg"];
UIButton *btnSideMenu = [UIButton buttonWithType:UIButtonTypeCustom];
[btnSideMenu setImage:sideImage forState:UIControlStateNormal];
[btnSideMenu setImage:sideImage forState:UIControlStateHighlighted];
[btnSideMenu setFrame:CGRectMake(0, 0, 20, 20)];
[btnSideMenu addTarget:self action:#selector(btnLeftMenuClicked) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *sideMenuItem = [[UIBarButtonItem alloc] initWithCustomView:btnSideMenu];
self.navigationItem.leftBarButtonItem=sideMenuItem;
You can create UIBarButtonItem using initWithCustomView
_leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
[_leftButton setImage:[UIImage imageNamed:#"back"] forState:UIControlStateNormal];
[_leftButton addTarget:self action:#selector(leftButton:) forControlEvents:UIControlEventTouchUpInside];
[_leftButton sizeToFit];
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:_leftButton];

changing text of custom navigation bar item

I am creating a custom UINavigationBar BarButtonItem as follows:
// Set up bar button items
UIButton *backButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 35.0f, 35.0f)];
[backButton setImage:[UIImage imageNamed:#"myimg.png"] forState:UIControlStateNormal];
[backButton addTarget:self action:#selector(myselector) forControlEvents:UIControlEventTouchUpInside];
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
This is working perfectly, but when I go to set the title of the UIButton or BarButtonItem, it won't show up. Any suggestions?
Tried...
[backButton setTitle:#"Edit" forState:UIControlStateNormal];
[backButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
And...
self.navigationItem.leftBarButtonItem.title = #"Edit";
without luck.
The problem is that image and title use same viewspace for display.
So when we use image with title first it show image and than title (right side of image).
If size of UIButton is same as image than we can't see Title.
A simple solution is set image in background. So try this:
[backButton setBackgroundImage:[UIImage imageNamed:#"myimg.png"] forState:UIControlStateNormal];
UIImage *image = [UIImage imageNamed:#"myimg.png"];
UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
[backButton setBackgroundImage:image forState:UIControlStateNormal];
[backButton setTitle:#"Edit" forState:UIControlStateNormal];
[backButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal]
backButton.frame=CGRectMake(0, 0, 35.0f, 35.0f);
UIBarButtonItem *barButton= [[[UIBarButtonItem alloc] initWithCustomView:backButton] autorelease];
self.toolbar.items = [NSArray arrayWithObject:barButton];

Custom background for default system items

i want to use the default bar button items like
UIBarButtonItem *editFavoritesButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemEdit target:self action:#selector(startEndEditing:)];
The problem is that i want to change the background of these button but
[editFavoritesButton setBackgroundImage:[UIImage imageNamed:#"barbuttonPlain.png"] forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
does not to the trick in this case.
UPDATED:
The idea is not creating a custom button since you need to handle the translations for this case.On the other hand i want to user compose and other sort of default images.
Replace your code from...
[editFavoritesButton setBackgroundImage:[UIImage imageNamed:#"barbuttonPlain.png"] forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
as like this...
[editFavoritesButton setImage:[UIImage imageNamed:#"barbuttonPlain.png"]];
Use this code to create custom UIBarButtonItem:
UIButton *mybutton = [UIButton buttonWithType:UIButtonTypeCustom];
[mybutton setFrame:CGRectMake(0.0f, 0.0f, 25.0f, 25.0f)];
[mybutton addTarget:self action:#selector(mybuttonFunction) forControlEvents:UIControlEventTouchUpInside];
[mybutton setImage:[UIImage imageNamed:#"myImage.png"] forState:UIControlStateNormal];
UIBarButtonItem *random = [[UIBarButtonItem alloc] initWithCustomView:mybutton];
Hope this will help you.
You can try like this If you have any question you can ask..
UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
backButton.frame = CGRectMake(0, 0, 83, 44);
backButton.adjustsImageWhenHighlighted=NO;
[backButton addTarget:self action:#selector(backMethod)
forControlEvents:UIControlEventTouchDown];
[backButton setImage:[UIImage imageNamed:#"img.png"]
forState:UIControlStateNormal];
UIBarButtonItem *barLeftInfoButton = [[UIBarButtonItem alloc] initWithCustomView:backButton];
self.navigationItem.leftBarButtonItem = barLeftInfoButton;
Add a custom button first and then add it to the UIBarButtonItem.
UIImage *buttonImage = [UIImage imageNamed:#"mainButton.png"];
//create the button and assign the image
self.yourButton = [UIButton buttonWithType:UIButtonTypeCustom];
[self.yourButton setFrame:CGRectMake(5,7,buttonImage.size.width,buttonImage.size.height)];
[self.yourButton setBackgroundImage:buttonImage forState:UIControlStateNormal];
self.yourButton.titleLabel.font = [UIFont boldSystemFontOfSize:12];
[self.yourButton setTitle:#"title" forState:UIControlStateNormal];
[self.yourButton addTarget:self action:#selector(startEndEditing:) forControlEvents:UIControlEventTouchUpInside];
// add image view
[self.navBar addSubview:self.yourButton];
//create a UIBarButtonItem with the button as a custom view
UIBarButtonItem *customBarItem = [[UIBarButtonItem alloc] initWithCustomView:self.yourButton];
self.navigationItem.leftBarButtonItem = customBarItem;
Please try to use this one ....
UIButton *editFavoritesButton = [[UIButton alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 55.0f, 30.0f)];
[editFavoritesButton setBackgroundColor:[UIColor clearColor]];
[editFavoritesButton setBackgroundImage:[UIImage imageNamed:#"barbuttonPlain.png"] forState:UIControlStateNormal];
[editFavoritesButton addTarget:self action:#selector(startEndEditing:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *leftBtn = [[UIBarButtonItem alloc] initWithCustomView:editFavoritesButton];
[self.navigationItem setLeftBarButtonItem:leftBtn];

Resources