1

When try to add a navigation button on left and right side , its not showing?is this is the code i used for, Whats wrong with this?

this is how i added the navigation bar in in appdelegate,

  UIViewController *viewController1 = [[logyFirstViewController alloc] initWithNibName:@"logyFirstViewController" bundle:nil];
  UIViewController *viewController2 = [[logySecondViewController alloc] initWithNibName:@"logySecondViewController" bundle:nil];
  UIViewController *viewController3 = [[record alloc] initWithNibName:@"record" bundle:nil];
  UIViewController *viewController4 = [[searchview alloc] initWithNibName:@"searchview" bundle:nil];
  UIViewController *viewController5 = [[profilde alloc] initWithNibName:@"profilde" bundle:nil];
 self.tabBarController = [[UITabBarController alloc] init];

self.tabBarController.viewControllers = @[viewController1, viewController2, viewController3, viewController4, viewController5];
navigationController = [[UINavigationController alloc] initWithRootViewController:self.tabBarController];

self.navigationController.navigationBar.barStyle=UIBarStyleDefault;

[navigationController.navigationBar setAlpha:0.8f];
 [self.window addSubview:[navigationController view]];





 button4 = [UIButton buttonWithType:UIButtonTypeCustom];
 [button4 setFrame:CGRectMake(0.0f, 0.0f, 25.0f, 25.0f)];
 [button4 addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
 [button4 setImage:[UIImage imageNamed:@"del.png"] forState:UIControlStateNormal];
 UIBarButtonItem *random1 = [[UIBarButtonItem alloc] initWithCustomView:button4];
 self.navigationController.navigationItem.leftBarButtonItem=random1;

please help.

MohanRaj S
  • 1,958
  • 4
  • 30
  • 54
  • use this line [[self navigationItem] setLeftBarButtonItem:random1]; – Sudha Tiwari Feb 05 '13 at 12:43
  • by the way, from Apples Documentation [[ - (id)initWithRootViewController:(UIViewController *)rootViewController ((rootViewController)) The view controller that resides at the bottom of the navigation stack. This object cannot be an instance of the UITabBarController class. ]] Look at this linnk : http://developer.apple.com/library/ios/#documentation/uikit/reference/UINavigationController_Class/Reference/Reference.html – Shumais Ul Haq Feb 05 '13 at 13:31
  • upload screenshot of your navigation bar – Rajneesh071 Feb 05 '13 at 14:47

6 Answers6

13

I see nothing wrong with your code just check your image or try with a rounded rect image to test

UIButton* button4 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button4 setFrame:CGRectMake(0.0f, 0.0f, 25.0f, 25.0f)];
[button4 addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *random1 = [[UIBarButtonItem alloc] initWithCustomView:button4];

and use self.navigationController.navigationBar.topItem.leftBarButtonItem=random1; instead of self.navigationController.navigationItem.leftBarButtonItem=random1;

zizoodiesel
  • 197
  • 1
  • 13
3

The problem is in the following line :

self.navigationController.navigationItem.leftBarButtonItem=random1;

Simply use

self.navigationItem.leftBarButtonItem=random1;
Shumais Ul Haq
  • 1,427
  • 1
  • 15
  • 26
0
UIButton *button4 =  [UIButton buttonWithType:UIButtonTypeCustom];
[button4 setImage:[UIImage imageNamed:@"del.png"] forState:UIControlStateNormal];
[button4 addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
[button4 setFrame:CGRectMake(0.0f, 0.0f, 25.0f, 25.0f)]];
    self.navigationItem.leftBarButtonItem = [[[UIBarButtonItem alloc] initWithCustomView:button4] autorelease];

use same method for leftBarButtonItem.Hope it works for you.Thanks

jamil
  • 2,419
  • 3
  • 37
  • 64
0

Hope it will work for you...

UIButton *button4 = [UIButton buttonWithType:UIButtonTypeCustom];
[button4 setFrame:CGRectMake(0.0f, 0.0f, 25.0f, 25.0f)];
[button4 addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
[button4 setImage:[UIImage imageNamed:@"del.png"] forState:UIControlStateNormal];
UIBarButtonItem *random1 = [[UIBarButtonItem alloc] initWithCustomView:button4];
[button4 release];

[[self navigationItem] setLeftBarButtonItem:random1];
[random1 release];
Sudha Tiwari
  • 2,499
  • 26
  • 50
0
UIBarButtonItem *barBtnItem = [[UIBarButtonItem alloc] initWithTitle:@"back" style:UIBarButtonItemStyleBordered target:self action:@selector(nextToPhoneAuth)];
self.navigationItem.backBarButtonItem = barBtnItem
Oh Seung Kwon
  • 431
  • 3
  • 11
0

please check that your navigation bar is provided hidden property and try with default back button once & let us know is it working or not.

Madhu
  • 1,542
  • 1
  • 14
  • 30