My Blog List

Android FirstAid Coding

A Small Help From a Small Heart
Powered by Blogger.

A software professional, who still likes to code, likes to blog and loves gadgets.

Friday 26 April 2013

How to add button in navigation bar in iphone/ipad/IOS



Adding system bar button

UIBarButtonItem *saveButton = [[UIBarButtonItem alloc]                             initWithBarButtonSystemItem:UIBarButtonSystemItemAdd
                               target:self 
                                style:UIBarButtonItemStyleBordered
                               action:@selector(postNewCase:)];
self.navigationItem.rightBarButtonItem = saveButton;
[saveButton release];

-(IBAction)postNewCase:(id)sender {
}

Adding image bar button

UIImage *buttonImage = [UIImage imageNamed:@"home_icon.png"];
UIButton *forwardButton = [UIButton      buttonWithType:UIButtonTypeCustom];
[forwardButton setBackgroundImage:buttonImage forState:UIControlStateNormal];
forwardButton.frame = CGRectMake(0, 0, buttonImage.size.width, buttonImage.size.height);
[forwardButton addTarget:self action:@selector(postNewCase:)
           forControlEvents:UIControlEventTouchUpInside];
   
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:forwardButton];
   

Adding plain button
UIBarButtonItem *postButton = [[UIBarButtonItem alloc] 
                               initWithTitle:@"Post"                                            
                               style:UIBarButtonItemStyleBordered 
                               target:self 
                               action:@selector(postNewCase:)];
self.navigationItem.rightBarButtonItem = postButton;
[postButton release];

Add background image to navigation bar

[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed: @"navigationBar.png"] forBarMetrics:UIBarMetricsDefault];

Add logo to navigation bar
 UIImage *image = [UIImage imageNamed: @"new_kj_logo-150.png"];
    UIImageView *imageView = [[UIImageView alloc] initWithImage: image];
   
    self.navigationItem.titleView = imageView;

Hide Navigation default back button
 self.navigationItem.hidesBackButton = YES;




1 comment: