0

I am changing my view controller using the following code:

 [self.slidingViewController performSegueWithIdentifier:@"FindScoresController" sender:self.slidingViewController];

The segue is PUSH

Now I want to add UINavigationBar Programatically. The code i am using for it is:

// Not required if viewController is pushed from previous viewController
UINavigationBar *navbar = [[UINavigationBar alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 64)];
UIColor *barColor = [[UIColor alloc]initWithRed:82/255.0f green:167/255.0f blue:192/255.0f alpha:1.0f];

if (floor(NSFoundationVersionNumber) <= NSFoundationVersionNumber_iOS_6_1) {
    // iOS 6.1 or earlier
    navbar.tintColor = barColor;
} else {
    // iOS 7.0 or later
    navbar.barTintColor = barColor;

   navbar.translucent = NO;
}
[self.view addSubview:navbar];

It adds nav bar successfully. Now I want to add left and right custom buttons with custom Image. But I am unable to do so.

Code i am using to add right UIBarButton item:

 UIBarButtonItem *flipButton = [[UIBarButtonItem alloc]
                               initWithTitle:@"Flip"
                               style:UIBarButtonItemStyleBordered
                               target:self
                               action:@selector(flipView:)];
self.navigationItem.rightBarButtonItem = flipButton;

But it doesn't show any effect.

I also tried this code:

UIBarButtonItem * item = [[UIBarButtonItem alloc] initWithCustomView:[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"yourimage2.jpg"]]];    
self.navigationItem.rightBarButtonItem = item; 

All these code i am writing in ViewDidLoad method.

abhishekkharwar
  • 3,529
  • 2
  • 18
  • 31
Rahul
  • 5,594
  • 7
  • 38
  • 92
  • 1
    Is there a reason for not using `UINavigationController`? – Jakub Vano May 05 '15 at 11:42
  • 1
    navigationItem is used when your viewcontroller is embedded in a navigation controller. when you setup your own navigation bar you have to use `- pushNavigationItem:animated:`... – André Slotta May 05 '15 at 11:42
  • @JakubVano I am using ECSlidingViewController .... and I have navigation controller also – Rahul May 05 '15 at 11:44
  • if you are using a navigationcontroller then you do not have to add your own navigation bar! remove that code and everything should be fine! – André Slotta May 05 '15 at 11:47
  • You are setting buttons on item of another bar. – Cy-4AH May 05 '15 at 11:48
  • @AndreSlotta I removed that code...now i hav the navigation bar.. but it shows in all other controllers also I want in only few of them – Rahul May 05 '15 at 11:52
  • 1
    you can show or hide the navigationbar in specific viewcontrollers by setting `self.navigationController.navigationBarHidden = YES or NO` – André Slotta May 05 '15 at 11:53
  • @AndreSlotta Thanx ..now i am getting Flip button..but how can i add custom image to it – Rahul May 05 '15 at 11:56
  • `[[UIBarButtonItem alloc] initWithImage...` – André Slotta May 05 '15 at 11:57
  • @AndreSlotta I am using this but how can i add selector to it – Rahul May 05 '15 at 11:59
  • 1
    the complete signature includes target and selector: `[[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"whatevertheimageisnamed"] style:UIBarButtonItemStylePlain target:self action:@selector(whateverselectoryouwannacall)]` – André Slotta May 05 '15 at 12:00
  • @AndreSlotta I am getting default navigation Back buton..can i assign custom image to it – Rahul May 05 '15 at 12:03
  • @AndreSlotta i am using this self.navigationItem.leftBarButtonItem.image =[UIImage imageNamed:@"btn_edit.png"]; but it is not working – Rahul May 05 '15 at 12:03
  • 1
    `[[UINavigationBar appearance] setBackIndicatorImage:[UIImage imageNamed:@"imagename"]];` – André Slotta May 05 '15 at 12:05
  • @AndreSlotta its not working :( – Rahul May 05 '15 at 12:09
  • sorry. this is the code: `[[UIBarButtonItem appearance] setBackButtonBackgroundImage:[UIImage imageNamed:@"imagename"] forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];` – André Slotta May 05 '15 at 12:12
  • @AndreSlotta sry to say but this also doesn't respond – Rahul May 05 '15 at 12:14
  • in my case it does. here you can find some more information: http://stackoverflow.com/questions/10275724/use-uinavigationbar-appearance-to-change-back-button-image – André Slotta May 05 '15 at 12:15
  • @AndreSlotta I am able to change the color of button title but can't able to change title color..how can i do it – Rahul May 05 '15 at 12:16
  • `self.navigationController.navigationBar.titleTextAttributes = @{NSForegroundColorAttributeName: [UIColor redColor]};` – André Slotta May 05 '15 at 12:18
  • @AndreSlotta thanx a ton...you saved my lots of hours :) – Rahul May 05 '15 at 12:29
  • @AndreSlotta are you there i have a problem... I want to pass a selector name as a parameter..but my app crashes...can you help – Rahul May 05 '15 at 13:17

0 Answers0