自定义按钮图像在导航栏iOS 7.1上不可见

时间:2014-03-27 09:43:11

标签: iphone ios7 uinavigationcontroller uibarbuttonitem ios7.1

我在导航栏上添加了自定义按钮。这里是代码现在我的问题是在iOS7中我能够看到带有文本的Button图像,而在iOS 7.1中图像不显示只有文本显示。 This is iOS 7.1 This 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];


}

3 个答案:

答案 0 :(得分:3)

您可以将自定义按钮分配给左侧或右侧按钮,而不是将其作为子视图添加到navigationView中,而这也是可行的

  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;

答案 1 :(得分:2)

[self.navigationItem setHidesBackButton:YES];

这确实有效,但你在这里犯了错误

[self.navigationController.navigationBar addSubview:backButtonView];

/////您创建自定义按钮的代码就在这里

  

self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]   initWithCustomView:返回按钮];

答案 2 :(得分:0)

创建自定义按钮并将其作为自定义视图添加到导航左栏按钮项目中,如下所示:

let btnShowMenu = UIButton()
let image: UIImage = UIImage(named: "requiredImageName")!
btnShowMenu.setImage(image, for: UIControlState.normal)
btnShowMenu.frame = CGRect(x: 0, y: 0, width: 15, height: 15)
btnShowMenu.addTarget(self, action: #selector(addAnAction(_:)), for: UIControlEvents.touchUpInside)
let customBarItem = UIBarButtonItem(customView: btnShowMenu)
self.navigationItem.leftBarButtonItem = customBarItem;