I am using Split View Controller, which has 2 View controllers on the second view controller i am suppose add two buttons on the right side of navigation controller.
i have used the following code to add one button which works:
UIBarButtonItem *barButton=[[UIBarButtonItem alloc] init];
[barButton setCustomView:btnShare];
self.navigationItem.rightBarButtonItem=barButton;
tried this link http://osmorphis.blogspot.in/2009/05/multiple-buttons-on-navigation-bar.html
but could not succeed.Please help me fix this.
Try this
NSMutableArray *arrRightBarItems = [[NSMutableArray alloc] init];
UIButton *btnSetting = [UIButton buttonWithType:UIButtonTypeCustom];
[btnSetting setImage:[UIImage imageNamed:#"settings.png"] forState:UIControlStateNormal];
btnSetting.frame = CGRectMake(0, 0, 32, 32);
btnSetting.showsTouchWhenHighlighted=YES;
[btnSetting addTarget:self action:#selector(onSettings:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc] initWithCustomView:btnSetting];
[arrRightBarItems addObject:barButtonItem];
UIButton *btnLib = [UIButton buttonWithType:UIButtonTypeCustom];
[btnLib setImage:[UIImage imageNamed:#"library.png"] forState:UIControlStateNormal];
btnLib.frame = CGRectMake(0, 0, 32, 32);
btnLib.showsTouchWhenHighlighted=YES;
[btnLib addTarget:self action:#selector(onMyLibrary:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *barButtonItem2 = [[UIBarButtonItem alloc] initWithCustomView:btnLib];
[arrRightBarItems addObject:barButtonItem2];
UIButton *btnRefresh = [UIButton buttonWithType:UIButtonTypeCustom];
[btnRefresh setImage:[UIImage imageNamed:#"refresh.png"] forState:UIControlStateNormal];
btnRefresh.frame = CGRectMake(0, 0, 32, 32);
btnRefresh.showsTouchWhenHighlighted=YES;
[btnRefresh addTarget:self action:#selector(onRefreshBtn:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *barButtonItem1 = [[UIBarButtonItem alloc] initWithCustomView:btnRefresh];
[arrRightBarItems addObject:barButtonItem1];
self.navigationItem.rightBarButtonItems=arrRightBarItems;
[self.navigationItem setRightBarButtonItem:[[UIBarButtonItem alloc] initWithCustomView:[GlobalMethods buttonWithImage:#"btn_home" heighlightImageName:#"btn_home_h" buttonFrame:CGRectMake(2, 1, 34, 34) selectorName:#selector(buttonHomeClicked:) target:self]]];
UIButton *tempButton = [UIButton buttonWithType:UIButtonTypeCustom];
[tempButton setFrame:CGRectMake(240, 5, 34, 34)];
[tempButton addTarget:target action:selectorName forControlEvents:UIControlEventTouchUpInside];
[tempButton setImage:[self getImageFromResource:normalImageName] forState:UIControlStateNormal];
[tempButton setImage:[self getImageFromResource:heighlightImageName] forState:UIControlStateHighlighted];
[tempButton setImage:[self getImageFromResource:#"btn_fav_h"] forState:UIControlStateSelected];
[self.navigationController.navigationBar addSubview:self.buttonFavorite];
create two uibarbuttonitem and add both in an array then add whole array in navigation bar
UIBarButtonItem *addButton = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:#selector(addAttachmentClicked:)];
UIBarButtonItem *sendButton = [[UIBarButtonItem alloc] initWithTitle:LS(#"Send") style:UIBarButtonItemStyleBordered target:self action:#selector(sendClicked:)];
self.navigationItem.rightBarButtonItems = #[addButton,sendButton];
try thi code
UIButton *btnLogOut = [UIButton buttonWithType:UIButtonTypeCustom];
btnLogOut.frame = CGRectMake(0, 0, 62, 31);
[btnLogOut setImage:[UIImage imageNamed:#"logout_new.png"] forState:UIControlStateNormal];
[btnLogOut addTarget:self action:#selector(logoutButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *logOutBarButton = [[UIBarButtonItem alloc] initWithCustomView:btnLogOut];
UIButton *btnError = [UIButton buttonWithType:UIButtonTypeCustom];
btnError.frame = CGRectMake(0, 0, 62, 31);
[btnError setImage:[UIImage imageNamed:#"list-icon.png"] forState:UIControlStateNormal];
[btnError addTarget:self action:#selector(logoutButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *errorButton = [[UIBarButtonItem alloc] initWithCustomView:btnError];
NSArray *navigationBarBtnArray=[NSArray arrayWithObjects:errorButton,logOutBarButton, nil];
self.navigationItem.rightBarButtonItems=navigationBarBtnArray;
Try this
UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithTitle:#"Done"
style:UIBarButtonItemStyleDone target:nil action:nil];
self.navigationItem.rightBarButtonItem = rightButton;
[rightButton release];
make a UIView adding as many buttons you want on it. then use
UIBarButtonItem *myBarbtn=[[UIBarButtonItem alloc] initWithCustomView:buttonsView];
self.navigationItem.rightBarButtonItem = myBarbtn;
Use segmented control for this
UISegmentedControl* segmentedControl = [[UISegmentedControl alloc] initWithItems:[NSArray array]];
[segmentedControl setMomentary:YES];
[segmentedControl insertSegmentWithImage:[UIImage imageNamed:#"sample1.png"] atIndex:0 animated:NO];
[segmentedControl insertSegmentWithImage:[UIImage imageNamed:#"sample2.png"] atIndex:1 animated:NO];
segmentedControl.autoresizingMask = UIViewAutoresizingFlexibleWidth;
segmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;
[segmentedControl addTarget:self action:#selector(segmentedAction:) forControlEvents:UIControlEventValueChanged];
UIBarButtonItem * segmentBarItem = [[UIBarButtonItem alloc] initWithCustomView: segmentedControl];
self.navigationItem.rightBarButtonItem = segmentBarItem;
Make an array of UIBarButtonItems and pass it to
- (void)setRightBarButtonItems:(NSArray *)items animated:(BOOL)animated
method of UINavigationItem.
Simply use an UISegmentedControl instead of a UIButton.
Then change the mode to "momentary" and add an action on change.
When the action is triggered, check the selectedSegmentIndex to know what segment was chosen.
Related
I am creating a UIPickerView, UIToolbar, UIBarButtonItem and UIButton programmatically. I'v set the custom picker as an inputView of a textView and everything works without problem except the Done button.
Does anyone have any idea of what the problem is?
The code I've used is:
// initialize picker
CGRect cgRect =[[UIScreen mainScreen] bounds];
CGSize cgSize = cgRect.size;
_picker = [[UIPickerView alloc] init];
_picker.frame=CGRectMake(0, 0, cgSize.width, cgSize.height);
_picker.showsSelectionIndicator = YES;
_picker.delegate = self;
// toolbar of picker
UIToolbar* toolbar = [[UIToolbar alloc] init];
toolbar.frame=CGRectMake(0, 0, cgSize.width, 35);
toolbar.barStyle = UIBarStyleBlackTranslucent;
UIBarButtonItem *flexibleSpaceLeft = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
UIButton *customButton = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 60, 33)];
[customButton setTitle:#"Done" forState:UIControlStateNormal];
[customButton addTarget:self action:#selector(doneClicked:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *barCustomButton =[[UIBarButtonItem alloc] initWithCustomView:customButton];
NSMutableArray * arr = [NSMutableArray arrayWithObjects:flexibleSpaceLeft, barCustomButton, nil];
[toolbar setItems:arr animated:YES];
self.txtTeam.inputView = _picker;
[_picker addSubview:toolbar];
And the doneClicked method is;
-(void)doneClicked
{
NSLog(#"Done button clicked.");
[self.txtTeam resignFirstResponder];
}
But the Done button is not clickable.
UIButton *customButton = [UIButton buttonWithType:UIButtonTypeCustom];
[customButton setFrame:CGRectMake(0, 0, 30, 60)];
[customButton setTitle:#"Done" forState:UIControlStateNormal];
[customButton addTarget:self action:#selector(doneClicked:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *barCustomButton =[[UIBarButtonItem alloc] initWithCustomView:customButton];
Try Like this..
You have set the selector for the button to call doneClicked: but the method is called doneClicked. Remove the : from the end of the method name in the selector and it should work.
Change your code to below:-
UIButton *customButton = [UIButton buttonWithType:UIButtonTypeCustom];
customButton.frame = CGRectMake(0, 0, 60, 33);
[customButton addTarget:self action:#selector(doneClicked) forControlEvents:UIControlEventTouchUpInside];
customButton.showsTouchWhenHighlighted = YES;
[customButton setTitle:#"Done" forState:UIControlStateNormal];
customButton.imageEdgeInsets = UIEdgeInsetsMake(10, 10, 10, 10);
UIBarButtonItem *barCustomButton =[[UIBarButtonItem alloc] initWithCustomView:customButton];
just comment the below your code and modify above
// UIButton *customButton = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 60, 33)];
// [customButton setTitle:#"Done" forState:UIControlStateNormal];
//[customButton addTarget:self action:#selector(doneClicked:) forControlEvents:UIControlEventTouchUpInside];
// UIBarButtonItem *barCustomButton =[[UIBarButtonItem alloc] initWithCustomView:customButton];
Below code is working fine in my application. But that leftbarbuttonitem is invisible. And I can't set background image to leftBarButtonItem. I did some thing wrong, but I don't know what is the mistake? Is it possible to make visible and set background image?
UIImageView *NavigationBarImage =[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 320, 44)];
NavigationBarImage.image=[UIImage imageNamed:#"mapbutton.png"];
[self.navigationController.navigationBar addSubview:NavigationBarImage];
self.navigationItem.hidesBackButton = YES;
UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonItemStyleBordered target:self action:#selector(goBack)];
self.navigationItem.leftBarButtonItem = backButton;
-(void)goBack
{
[self.navigationController popViewControllerAnimated:YES];
}
If you want to add a backgroundImage to your navigation item you have to add a custom button, like bellow:
UIButton *tempButton = [UIButton buttonWithType:UIButtonTypeCustom];
tempButton.frame = CGRectMake(0, 0, 50, 30);
tempButton.showsTouchWhenHighlighted = YES;
tempButton.backgroundColor = [UIColor clearColor];
[tempButton setBackgroundImage:[UIImage imageNamed:#"back.png"] forState:UIControlStateNormal];
[tempButton setTitle:#"Back" forState:UIControlStateNormal];
[tempButton addTarget:self action:#selector(goBack) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *backBtn = [[UIBarButtonItem alloc]initWithCustomView:tempButton];
self.navigationItem.leftBarButtonItem = backBtn;
And to add a background Image to navigation bar you can use the following code
[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:#"header.png"] forBarMetrics:UIBarMetricsDefault];
UIButton *home = [UIButton buttonWithType:UIButtonTypeCustom];
UIImage *homeImage = [UIImage imageNamed:#"YOUR_IMAGE.png"];
[home setBackgroundImage:homeImage forState:UIControlStateNormal];
[home addTarget:self action:#selector(your_Action)
forControlEvents:UIControlEventTouchUpInside];
home.frame = CGRectMake(0, 0, 69, 26);
UIBarButtonItem *button2 = [[UIBarButtonItem alloc] initWithCustomView:home];
[[self navigationItem] setLeftBarButtonItem:button2];
[button2 release];
button2 = nil;
I'm trying for hours to make a left button work properly and mimic a back button.
My code to create the button:
UIBarButtonItem *backButton = [self customBarButton:#"back_button" imageHiglighted:#"settings_button_highlighted" x:20 y:0 widthDivider:2.6 heightDivider:2.6];
backButton.target = self;
backButton.action = #selector(buttonPressed:);
self.navigationItem.leftBarButtonItem = backButton;
Here the method called to create custom button:
- (UIBarButtonItem *)customBarButton:(NSString *)imageDefault imageHiglighted:(NSString *)imageHighlighted x:(float)x y:(float)y widthDivider:(float)widthDivider heightDivider:(float)heightDivider {
UIImage *customImageDefault = [UIImage imageNamed:imageDefault];
UIImage *customImageHighlighted = [UIImage imageNamed:imageHighlighted];
CGRect frameCustomButton = CGRectMake(x, y, customImageDefault.size.width/widthDivider, customImageDefault.size.height/heightDivider);
UIButton *customButton = [[UIButton alloc] initWithFrame:frameCustomButton];
[customButton setBackgroundImage:customImageDefault forState:UIControlStateNormal];
[customButton setBackgroundImage:customImageHighlighted forState:UIControlStateHighlighted];
UIBarButtonItem *barCustomButton =[[UIBarButtonItem alloc] initWithCustomView:customButton];
return barCustomButton;
}
And the action:
-(void)buttonPressed:(id) sender{
NSLog(#"Entered");
SearchViewController *ViewController = [[SearchViewController alloc] init];
[self.navigationController pushViewController:ViewController animated:YES];
}
So I was able to make it with a simple UIButton but not with a UIButtonBarItem and I really don't know what's going on with it.
If you could help me I'd be very grateful.
Thanks.
Do this add selector to custom button as it is view of bar buttom:
[customButton addTarget:self action:#selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
EDIT : Note : the target and action of the UIBarButtonItem apply to custom views.
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setImage:[UIImage imageNamed:#"goback.png"] forState:UIControlStateNormal];
[button addTarget:self action:#selector(//HERE ! i don't know put what thing !) forControlEvents:UIControlEventTouchUpInside];
[button setFrame:CGRectMake(0, 0, 32, 32)];
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];
TRY THIS
self.navigationItem.leftBarButtonItem=[[UIBarButtonItem alloc] initWithCustomView:[GlobalMethods buttonWithImage:#"top-nav-back" heighlightImageName:nil buttonFrame:CGRectMake(102, 1, 50, 30) selectorName:#selector(btnBackClicked:) target:self]];
+ (UIButton*)buttonWithImage:(NSString*)normalImageName heighlightImageName:(NSString*)heighlightImageName buttonFrame:(CGRect)buttonFrame selectorName:(SEL)selectorName target:(id)target
{
UIButton *objButton = [UIButton buttonWithType:UIButtonTypeCustom];
[objButton setFrame:buttonFrame];
[objButton addTarget:target action:selectorName forControlEvents:UIControlEventTouchUpInside];
[objButton setImage:[GlobalMethods getImageFromResourceBundle:normalImageName] forState:UIControlStateNormal];
if(heighlightImageName)
[objButton setImage:[GlobalMethods getImageFromResourceBundle:heighlightImageName] forState:UIControlStateDisabled];
if(heighlightImageName)
[objButton setImage:[GlobalMethods getImageFromResourceBundle:heighlightImageName] forState:UIControlStateSelected];
return objButton;
}
Try this..
[customButton addTarget:self action:#selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
in your customBarButton method..
I mean to say give selector to your button instead of bar button item..
Check and reply..
Just put this line in your code..
[backButton addTarget:self action:#selector(backBtnPressed:) forControlEvents:UIControlEventTouchUpInside];
This worked for me
UIButton * tmpButton = [self generateButtonwithImageName:#"Back.png" andSize:CGRectMake(0, 0, 55, 30)];
UIBarButtonItem * backButton = [[UIBarButtonItem alloc] initWithCustomView:tmpButton];
[tmpButton addTarget:self action:#selector(reload) forControlEvents:UIControlEventTouchUpInside];
self.navigationItem.leftBarButtonItem=backButton;
-(UIButton*)generateButtonwithImageName :(NSString*)imageName andSize:(CGRect)rect{
UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame=rect;
[button setBackgroundImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];
return button;
}
//working for me
if([version floatValue] < 7.0)
{
[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:#"navi-bar.png"] forBarMetrics:UIBarMetricsDefault];
self.navigationController.navigationBar.opaque = YES;
closeButton = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 35, 30)];
[closeButton setImage:[UIImage imageNamed:#"btn_end.png"] forState:UIControlStateNormal];
[closeButton setImage:[UIImage imageNamed:#"btn_end_on.png"] forState:UIControlStateHighlighted];
[closeButton addTarget:self action:#selector(actionClose) forControlEvents:UIControlEventTouchUpInside];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:closeButton];
}
else{
/*add button back to navigation*//*add buttom back to navigation*/
UIBarButtonItem *itemBack = [[UIBarButtonItem alloc]initWithTitle:#"完了" style:UIBarButtonItemStylePlain target:self action:#selector(actionClose)];
self.navigationItem.rightBarButtonItem = itemBack;
}
I'm developing a small application. I need to create three buttons in a subclass. One button is add, another one is search and the last one is back. I also create left and right buttons. But I can't create search button in the center of the navigation bar. How can I create it? My code is:
- (void)viewDidLoad
{
[super viewDidLoad];
UIBarButtonItem *flipButton = [[UIBarButtonItem alloc] initWithTitle:#"Flip"
style:UIBarButtonItemStyleBordered
target:self
action:#selector(flipView)];
self.navigationItem.rightBarButtonItem = flipButton;
[flipButton release];
UIBarButtonItem *flipButtons = [[UIBarButtonItem alloc]
initWithTitle:#"Add"
style:UIBarButtonItemStyleBordered
target:self
action:#selector(addbuttonview)];
self.navigationItem.leftBarButtonItem = flipButtons;
[flipButtons release];
}
How to create a middle button in the navigation bar? Please help me.
Below is the code to use segment control in navigation bar programatically
NSArray* arr = [[NSArray alloc] initWithObjects:[UIImage imageNamed:#"Log_Button.png"], [UIImage imageNamed:#"Chart_Button.png"], nil];
segmentedControl = [[UISegmentedControl alloc] initWithItems:arr];
[segmentedControl addTarget:self action:#selector(action) forControlEvents:UIControlEventValueChanged];
[segmentedControl setSegmentedControlStyle:UISegmentedControlStyleBar];
[arr release];
UIBarButtonItem *rb = [[UIBarButtonItem alloc] initWithCustomView:segmentedControl];
[self.navigationItem setRightBarButtonItem:rb];
[rb release];
UIButton *btnBack = [UIButton buttonWithType:UIButtonTypeCustom];
btnBack.frame = CGRectMake(10, 4, 100, 50);
[btnBack setImage:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:#"button_back" ofType:#"png"]] forState:UIControlStateNormal];
[btnBack addTarget:self action:#selector(btnBackPressed:) forControlEvents:UIControlEventTouchUpInside];
[self.navigationController.navigationBar addSubview:btnBack];
UIButton *btnHome = [UIButton buttonWithType:UIButtonTypeCustom];
btnHome.frame = CGRectMake(115, 4, 38, 30);
[btnHome setImage:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:#"header_icon_home" ofType:#"png"]] forState:UIControlStateNormal];
[btnHome addTarget:self action:#selector(btnHomePressed:) forControlEvents:UIControlEventTouchUpInside];
[self.navigationController.navigationBar addSubview:btnHome];
UIButton *searchBtn=[UIButton buttonWithType:UIButtonTypeCustom];
searchBtn.frame=CGRectMake(175, 2, 60, 40);
[searchBtn addTarget:self action:#selector(seachbtnPressed:) forControlEvents:UIControlEventTouchDown];
[searchBtn setImage:[UIImage imageNamed:#"Search.jpg"] forState:0];
[self.navigationController.navigationBar addSubview:searchBtn];
take three UIButtons and Add to navigationBar.set Frames Images As Per Ur Design.
i was working with navigation bar button items.i was using the following code to do so
UIBarButtonItem *btnSave = [[UIBarButtonItem alloc]
initWithTitle:#"Save"
style:UIBarButtonItemStyleBordered
target:self
action:#selector(save_Clicked:)];
self.navigationItem.rightBarButtonItem = btnSave;
[btnSave release];
UIBarButtonItem *btnCancel = [[UIBarButtonItem alloc]
initWithTitle:#"Cancel"
style:UIBarButtonItemStyleBordered
target:self
action:#selector(save_Clicked)];
self.navigationItem.leftBarButtonItem = btnCancel;
[btnCancel release];
my question is how to add another button just adjacent to the left bar button item.
thanks in advance
To do this you need to create a toolbar then keep adding UIButton to it, then set the toolbar as the leftBarButton
something like this:
UIToolbar* tools = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 250, 44)];
tools.tintColor = [UIColor clearColor];
[tools setTranslucent:YES];
NSMutableArray* buttons = [[NSMutableArray alloc] initWithCapacity:9];
UIImage *myImage = [UIImage imageNamed:#"AL_HomeMod_Icon.png"];
UIButton *myButton = [UIButton buttonWithType:UIButtonTypeCustom];
[myButton setImage:myImage forState:UIControlStateNormal];
myButton.showsTouchWhenHighlighted = YES;
myButton.frame = CGRectMake(0.0, 0.0, myImage.size.width, myImage.size.height);
[myButton addTarget:self action:#selector(clickViewHomeMod) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *bi = [[UIBarButtonItem alloc]
initWithCustomView:myButton];
[buttons addObject:bi];
[bi release];
myImage = [UIImage imageNamed:#"AL_History_Icon.png"];
myButton = [UIButton buttonWithType:UIButtonTypeCustom];
[myButton setImage:myImage forState:UIControlStateNormal];
myButton.showsTouchWhenHighlighted = YES;
myButton.frame = CGRectMake(0.0, 0.0, myImage.size.width, myImage.size.height);
[myButton addTarget:self action:#selector(clickViewHistory) forControlEvents:UIControlEventTouchUpInside];
bi = [[UIBarButtonItem alloc]
initWithCustomView:myButton];
[buttons addObject:bi];
[bi release];
myImage = [UIImage imageNamed:#"AL_RX_Icon.png"];
myButton = [UIButton buttonWithType:UIButtonTypeCustom];
[myButton setImage:myImage forState:UIControlStateNormal];
myButton.showsTouchWhenHighlighted = YES;
myButton.frame = CGRectMake(0.0, 0.0, myImage.size.width, myImage.size.height);
[myButton addTarget:self action:#selector(clickViewCustomPopView2) forControlEvents:UIControlEventTouchUpInside];
bi = [[UIBarButtonItem alloc]
initWithCustomView:myButton];
[buttons addObject:bi];
[bi release];
myImage = [UIImage imageNamed:#"AL_User_Icon.png"];
myButton = [UIButton buttonWithType:UIButtonTypeCustom];
[myButton setImage:myImage forState:UIControlStateNormal];
myButton.showsTouchWhenHighlighted = YES;
myButton.frame = CGRectMake(0.0, 0.0, myImage.size.width, myImage.size.height);
[myButton addTarget:self action:#selector(clickViewCustomPopView:) forControlEvents:UIControlEventTouchUpInside];
bi = [[UIBarButtonItem alloc]
initWithCustomView:myButton];
[buttons addObject:bi];
popButton = myButton;
[bi release];
// stick the buttons in the toolbar
[tools setItems:buttons animated:NO];
[buttons release];
// and put the toolbar in the nav bar
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:tools];
[tools release];
hope that help
Pondd
Create a button as
UIBarButtonItem *logoutButton = [[UIBarButtonItem alloc]
initWithImage:[UIImage imageNamed:#"logout.png"]
style:UIBarButtonItemStylePlain
target:self action:#selector(doLogout)];
Add this button to right of navigation bar
self.navigationItem.rightBarButtonItem = logoutButton;
or add this button to left side of navigation bar
self.navigationItem.leftBarButtonItem = logoutButton;
doLogout is a function which will be called on touch logout button
I achieved my task by using the following code :
UIToolbar *tools=[[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, 150, 44)];
tools.backgroundColor=[UIColor clearColor];
[tools setTranslucent:YES];
UIBarButtonItem *optionBtn=[[UIBarButtonItem alloc]initWithTitle:#"Options" style:UIBarButtonItemStyleBordered target:self action:#selector(optionPressed:)];
UIBarButtonItem *doneBtn=[[UIBarButtonItem alloc]initWithTitle:#"Done" style:UIBarButtonItemStyleBordered target:self action:#selector(donePressed:)];
NSArray *buttons=[NSArray arrayWithObjects:optionBtn,doneBtn, nil];
[tools setItems:buttons animated:NO];
self.navigationItem.rightBarButtonItem=[[UIBarButtonItem alloc]initWithCustomView:tools];
NOTE:From IOS 5.0 onwards, apple has done it lot easier . It can be done as
self.navigationItem.rightBarButtonItems=[NSArray arrayWithObjects:optionBtn,doneBtn, nil];
Create a custom view with two buttons and use UIBarButtonItem's initWithCustomView: initializer. That should do it.