Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom button Image not visible on navigation bar iOS 7.1

I had added custom button on my navigation bar. here is the code now my problem is that in iOS7 I am able to see back Button image with text, while in iOS 7.1 the image is not displaying only text is displaying. This is iOS 7.1This is iOS 7

-(void)addBackButton{

    self.navigationItem.hidesBackButton = YES;

    [backButtonView removeFromSuperview];

   if (backButtonView) {
       [backButtonView release];
      backButtonView = nil;
   }

    backButtonView = [[UIView alloc] initWithFrame:CGRectMake(0,6,70,30)];

    NSString *strText = @"Back";

     btnBack = [[UIButton alloc] initWithFrame:CGRectMake(2,0, 70, 31)];

     [btnBack setTitle:strText forState:UIControlStateNormal];
     btnBack.titleLabel.font = [UIFont fontWithName:@"HelveticaNeue-Medium" size:17];
     btnBack.titleLabel.textColor = IOS7ColorBtnBackText;
     [btnBack setImage:[UIImage imageNamed:@"Left_Arrow.png"] forState:UIControlStateNormal];

     [btnBack addTarget:self action:@selector(btnBack:) forControlEvents:UIControlEventTouchUpInside];

     [backButtonView addSubview:btnBack];
     [btnBack release];

      [self.navigationController.navigationBar addSubview:backButtonView];


}
like image 610
Bhupesh Avatar asked Mar 27 '14 09:03

Bhupesh


2 Answers

Instead of adding it as subview in navigationView you can assign custom button to left or right button and this too works

  UIButton *backbtn=[[UIButton alloc]initWithFrame:CGRectMake(20,5,42, 42)];
  [backbtn setImage:[UIImage imageNamed:@"arrow.png"] forState:UIControlStateNormal];
  [backbtn setTitle:@"Login" forState:UIControlStateNormal];
  [backbtn addTarget:self action:@selector(didTapBackButton:) forControlEvents:UIControlEventTouchUpInside];
  UIBarButtonItem *barbtn=[[UIBarButtonItem alloc]initWithCustomView:backbtn];
  self.navigationItem.leftBarButtonItem=barbtn;
like image 169
Yohan Avatar answered Oct 24 '22 21:10

Yohan


[self.navigationItem setHidesBackButton:YES];

This do work, but you made mistake here

[self.navigationController.navigationBar addSubview:backButtonView];

///// Your code to create custom button goes here

self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:BackButton];

like image 21
DivineDesert Avatar answered Oct 24 '22 22:10

DivineDesert