i want to add buttons on navigation bar but its not loading on bar.Please help.Following is my code.please tell me where i am going wrong.The view i am using is landscape
- (void)viewDidLoad {
[super viewDidLoad];
CGRect rect = CGRectMake(0.0, 0.0, 480.0, 32.0);
navBar = [[UINavigationBar alloc] initWithFrame:rect];
navBar.items = self.navigationController.navigationBar.items;
navBar.delegate = self;
//[navBar release];
navBar.tintColor =[UIColor blackColor]
UIBarButtonItem * rightButton = [[UIBarButtonItem alloc]initWithTitle:#"Photo" style:UIBarButtonItemStyleBordered target:self action:#selector(takePhoto)];
UIBarButtonItem * backButton = [[UIBarButtonItem alloc]initWithTitle:#"Camera List" style:UIBarButtonItemStyleBordered target:self action:#selector(cancel:)];
self.navigationItem.rightBarButtonItem = rightButton;
self.navigationItem.leftBarButtonItem = backButton;
//navBar.topItem.rightBarButtonItem = rightButton;
// navBar.topItem.leftBarButtonItem = backButton;
[self.view addSubview:navBar];
[backButton release];
}
try this
(void)viewDidLoad {
[super viewDidLoad];
CGRect rect = CGRectMake(0.0, 0.0, 480.0, 32.0);
navBar = [[UINavigationBar alloc] initWithFrame:rect];
navBar.items = self.navigationController.navigationBar.items;
navBar.delegate = self;
UINavigationItem *navigationItem = [UINavigationItem alloc];
[navBar pushNavigationItem:navigationItem animated:NO];
[navigationItem release];
UIBarButtonItem * rightButton = [[UIBarButtonItem alloc]initWithTitle:#"Photo" style:UIBarButtonItemStyleBordered target:self action:#selector(takePhoto)];
UIBarButtonItem * backButton = [[UIBarButtonItem alloc]initWithTitle:#"Camera List" style:UIBarButtonItemStyleBordered target:self action:#selector(cancel:)];
self.navigationItem.rightBarButtonItem = rightButton;
self.navigationItem.leftBarButtonItem = backButton;
[self.view addSubview:navBar];
[backButton release];
[rightButton release];
}
Related
I try to present a modal view (InfoViewController) from a navigation view controller(DateViewController).
I add a toolbar on top of InfoViewContoller's view. Now I want to add a title "Info" and a "Done" button on the toolbar.(The Done button will perform the infoDismissAction method)
Can anyone give me som tips? Thanks a lot!
Here's code of DateViewController.h
#import <UIKit/UIKit.h>
#import "InfoViewController.h"
#interface DateViewController : UIViewController
{
InfoViewController *infoViewController;
}
#property (nonatomic, retain) InfoViewController *infoViewController;
#end
DateViewController.m
- (IBAction)modalViewAction:(id)sender{
if (self.infoViewController == nil)
self.infoViewController = [[[InfoViewController alloc] initWithNibName:
NSStringFromClass([InfoViewController class]) bundle:nil] autorelease];
[self presentModalViewController:self.infoViewController animated:YES];
}
- (void)dealloc{
if (self.infoViewController != nil)
{
[infoViewController release];
}
[super dealloc];
}
- (void)viewDidLoad{
self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithTitle:#"Info" style:UIBarButtonSystemItemPlay target:self action:#selector(modalViewAction:)] autorelease];
[modalBarButtonItem release];
[super viewDidLoad];
}
Here's InfoViewController.m
- (IBAction)infoDismissAction:(id)sender{
[self.parentViewController dismissModalViewControllerAnimated:YES];
}
- (void)viewDidLoad {
UIToolbar *toolBar;
toolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 50)];
toolBar.frame = CGRectMake(0, 0, 320, 50);
toolBar.barStyle = UIBarStyleDefault;
[toolBar sizeToFit];
[self.view addSubview:toolBar];
[toolBar release];
[backButton release];
[super viewDidLoad];
}
Try this code.
- (void)viewDidLoad {
UIToolbar *toolBar;
toolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 50)];
toolBar.frame = CGRectMake(0, 0, 320, 50);
toolBar.barStyle = UIBarStyleDefault;
[toolBar sizeToFit];
UIBarButtonItem *flexibleSpace = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil] autorelease];
UIBarButtonItem *infoButton = [[[UIBarButtonItem alloc] initWithTitle:#"INFO" style:UIBarButtonItemStyleBordered target:self action:#selector(InfoAction:)] autorelease];
UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithTitle:#"DONE" style:UIBarButtonItemStyleBordered target:self action:#selector(doneAction:)];
NSArray *barButton = [[NSArray alloc] initWithObjects:flexibleSpace,infoButton,flexibleSpace,doneButton,nil];
[toolBar setItems:barButton];
[self.view addSubview:toolBar];
[toolBar release];
[barButton release];
barButton = nil;
[super viewDidLoad];
}
I'd recommend setting up another UINavigationController with your InfoViewController and present the navigation controller as your modal view.
To answer your question you'd want to fill in your UIToolbar like this:
- (void)viewDidLoad {
[super viewDidLoad];
UIToolbar *toolBar;
toolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 50)];
toolBar.frame = CGRectMake(0, 0, 320, 50);
toolBar.barStyle = UIBarStyleDefault;
[toolBar sizeToFit];
[self.view addSubview:toolBar];
[toolBar release];
UIBarButtonItem* bbiInfo = [[UIBarButtonItem alloc] initWithTitle:#"Info" style:UIBarButtonItemStyleBordered target:self action:#selector(tappedInfoButton)];
UIBarButtonItem* flexibleSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
UIBarButtonItem* bbiDone = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:#selector(tappedDoneButton)];
NSArray* items = [[NSArray alloc] initWithObjects:bbiInfo, flexibleSpace, bbiDone, nil];
[toolBar setItems:items];
[items release];
[bbiInfo release];
[flexibleSpace release];
[bbiDone release];
}
Thanks to all. I found really working code. It looks like:
- (void)viewDidLoad {
[super viewDidLoad];
UIButton* infoButton = [UIButton buttonWithType: UIButtonTypeInfoLight];
[infoButton addTarget:self action:#selector(settingsClick) forControlEvents:UIControlEventTouchDown];
UIBarButtonItem* theSettingsButton =[[UIBarButtonItem alloc]initWithCustomView:infoButton];
[self.toolbar setItems:[NSArray arrayWithObjects:theSettingsButton,nil]];
[theSettingsButton release];
}
This works:
- (void)viewDidLoad {
[super viewDidLoad];
UIBarButtonItem *theSettingsButton = [[[UIBarButtonItem alloc]
initWithTitle:#"Settings" style:UIBarButtonItemStyleBordered
target:self action:#selector(settingsClick)] autorelease];
}
-(void)settingsClick
{
NSLog(#"Hello");
}
You hadn't closed your viewDidLoad method.
UIBarButtonItem *theSettingsButton = [[[UIBarButtonItem alloc]
initWithTitle:#"Settings" style:UIBarButtonItemStyleBordered
target:self action:#selector(settingsClick)]autorelease];
NSMutableArray * arr = [NSMutableArray arrayWithObjects:theSettingsButton, nil];
[self.toolbar setToolbarItems:arr animated:YES];
try this
toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 380, 320, 40)];
UIBarButtonItem *theSettingsButton = [[[UIBarButtonItem alloc]
initWithTitle:#"Settings" style:UIBarButtonItemStyleBordered
target:self action:#selector(settingsClick)]autorelease];
NSArray * arr = [NSArray arrayWithObjects:theSettingsButton, nil];
[toolbar setItems:arr animated:YES];
[self.view addSubview:toolbar];
[toolbar release];
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.
Below is my coding
// Navigation logic may go here -- for example, create and push another view controller.
NextViewController *nextViewController =
[[NextViewController alloc] initWithNibName:#"NextViewController" bundle:nil];
[self.navigationController pushViewController:nextViewController animated:YES];
[nextViewController release];
UIImage *backImage=[UIImage imageNamed:#"backbutton.png"];
UIBarButtonItem * backBarButtonItem = [[UIBarButtonItem alloc]
initWithImage:backImage style:UIBarButtonItemStylePlain target:nil
action:nil];
self.navigationItem.backBarButtonItem = backBarButtonItem;
[backBarButtonItem release];
}
Here, from my own project. (I am doing this in viewDidLoad)
UIImage *image = [UIImage imageNamed:#"btn-custom-back.png"];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setBackgroundImage:image forState:UIControlStateNormal];
button.frame = CGRectMake(0.0, 0.0, image.size.width, image.size.height);
[button addTarget:self action:#selector(popView) forControlEvents:UIControlEventTouchUpInside];
UIView *buttonView = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, image.size.width, image.size.height)];
[buttonView addSubview:button];
UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithCustomView:buttonView];
[buttonView release];
self.navigationItem.leftBarButtonItem = backItem;
[backItem release];
And the method:
- (void)popView {
[self.navigationController popViewControllerAnimated:YES];
}
The modification to the back button has to be done before you push the new view controller onto the navigation stack.
So just move your code around to look like this:
UIImage *backImage=[UIImage imageNamed:#"backbutton.png"];
UIBarButtonItem * backBarButtonItem = [[UIBarButtonItem alloc]
initWithImage:backImage style:UIBarButtonItemStylePlain target:nil
action:nil];
self.navigationItem.backBarButtonItem = backBarButtonItem;
[backBarButtonItem release];
NextViewController *nextViewController =
[[NextViewController alloc] initWithNibName:#"NextViewController" bundle:nil];
[self.navigationController pushViewController:nextViewController animated:YES];
[nextViewController release];
i got the following code:
- (id)init {
if (self = [super init]) {
self.title = #"please wait";
UIBarButtonItem *favorite = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"star.png"] style:UIBarButtonItemStylePlain target:self action:#selector(buttonFavoriteClicked:)];
self.navigationItem.rightBarButtonItem = favorite;
}
return self;
}
but my button looks still like a Button with UIBarButtonItemStyleBordered
is there a way to set a button with plain style at this position?
Create a UIButton in interface builder, make it look like exactly what you want. Create an outlet for in in your .h:
IBOutlet UIButton * _myButton;
Then set it as your right bar button item using a custom view, which will eliminate the border:
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:_myButton];
This works because UIButton is a subclass of UIView.
Try this instead:
- (id)init {
if (self = [super init]) {
self.title = #"please wait";
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 25, 25)];
[button setImage:[UIImage imageNamed:#"star.png"] forState:UIControlStateNormal];
[button addTarget:self action:#selector(buttonFavoriteClicked) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *favorite = [[UIBarButtonItem alloc] initWithCustomView:button];
[button release];
self.navigationItem.rightBarButtonItem = favorite;
}
return self;
}
Hope it helps.
Or do this in code without IB:
// add setting button to nav bar
UIImage* settingsGearImg = [UIImage imageNamed:#"settings_gear.png"];
CGRect frameimg = CGRectMake(0, 0, settingsGearImg.size.width, settingsGearImg.size.height);
UIButton *settingsButton = [[UIButton alloc] initWithFrame:frameimg];
[settingsButton setBackgroundImage:settingsGearImg forState:UIControlStateNormal];
[settingsButton addTarget:self action:#selector(settingsButtonAction) forControlEvents:UIControlEventTouchUpInside];
[settingsButton setShowsTouchWhenHighlighted:YES];
UIBarButtonItem *settingButtonItem =[[UIBarButtonItem alloc] initWithCustomView:settingsButton];
self.navigationItem.leftBarButtonItem = settingButtonItem;
Results in:
When using this icon image (it's white on a white background so isn't visible here - link is: http://i.stack.imgur.com/lk5SB.png):
It's weird but it works. Say "No" to images/outlets! You shouldn't even set the UIBarButtonItemStylePlain property. The trick is to place button into UIToolBar with some special attributes:
UINavigationItem *item = [[UINavigationItem alloc] initWithTitle:#"Cool plain bar"];
UIToolbar *tools = [[UIToolbar alloc]
initWithFrame:CGRectMake(0.0f, 0.0f, 44.0f, 44.01f)];
tools.clipsToBounds = NO;
tools.barStyle = -1; // clear background
NSMutableArray *buttons = [[NSMutableArray alloc] initWithCapacity:1];
UIBarButtonItem *bi = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh target:self action:#selector(refreshTableView)];
[buttons addObject:bi];
[tools setItems:buttons animated:NO];
UIBarButtonItem *rightButtons = [[UIBarButtonItem alloc] initWithCustomView:tools];
item.rightBarButtonItem = rightButtons;
item.hidesBackButton = YES;
[myCoolNavigationBar pushNavigationItem:item animated:NO];
Although Frank is right, this code should be in viewDidLoad, the issue here is what BarButtons look like. If you want it to look different, you need to use a different type of UIView. You can add a UIButton to a UIToolbar just fine.
-(void)viewDidLoad {
[super viewDidLoad];
self.title = #"please wait";
self.navigationItem.rightBarButtonItem = [[[UIButton alloc] init] autorelease];
}
Edit:
Sorry, you wanted a plain UIBarButtonItem. I don't believe you can do this with a UINavigationBar. You could try adding a UserInteractionEnabled UILabel as the rightBarButtonItem, I'm not sure if it will work or not.
Seems this isn't currently possible.
sbwoodside has a solution.
Why not try UI7Kit? Works well for me, easy to use.
UIImage *img = [UIImage imageNamed:#"white_star.png"];
CGSize itemSize = CGSizeMake(20, 20);
UIGraphicsBeginImageContext(itemSize);
CGRect imageRect = CGRectMake(0.0, 0.0, itemSize.width, itemSize.height);
[img drawInRect:imageRect];
img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIBarButtonItem *barButton = [[UIBarButtonItem alloc ] initWithImage:img style:UIBarButtonSystemItemAdd target:self action:#selector(favoriteButtonClicked)];
barButton.style = UIBarButtonItemStyleBordered;
Try this,
UIBarButtonItem *barBtn = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:#"more.png"] style:UIBarButtonItemStylePlain target:self action:#selector(didPressButton)];