backBarButtonItem未显示

时间:2015-08-10 14:43:43

标签: ios objective-c iphone uinavigationcontroller uinavigationbar

我知道当另一个视图控制器被推入堆栈时,会显示viewcontroller's导航项的backBarButtonItem,这是从顶部开始的第二个viewcontroller

我的viewcontroller A在viewDidLoad

中有以下内容
{
 [super viewDidLoad];
 self.navigationController.navigationBarHidden = NO;
 [self.navigationItem setBackBarButtonItem:[UIBarButtonItem itemWithImageNamed:@"ic_header_slide" selectedImage:nil target:nil action:nil]];
}

当我按viewcontroller B时,此自定义后退按钮未显示,而是显示iOS创建的默认后退按钮。

A展开UITableViewController,B展开UIViewController。我没有在任何这些navigationItem中设置leftBarButtonItemleftBarButtonItemsrightBarButtonItemrightBarButtonItems

EDIT 我已经阅读了有关设置leftBarButtonItems的内容。在B上设置leftbarbuttonitems。但我认为在A上设置backBarButtonItem是正确的方法。它也在文档中提到但在我的情况下不起作用。我想问一下backBarButtonItem中是否有bug,或者我对它的工作方式有一些误解,我没有正确地做到这一点。

3 个答案:

答案 0 :(得分:0)

要隐藏导航栏使用的默认后退按钮,

 self.navigationItem.hidesBackButton=TRUE;

还可以使用以下方法添加自定义BarButtons

- (NSArray*)getLeftNavButtons:(NSString*)image andTarget:(id)target andFrame:(CGRect)frame andSpace:(int)fixedSpace
{
   UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
   button.frame = frame;
   button.clipsToBounds = YES;
   [button setBackgroundImage:[UIImage imageNamed:image] forState:UIControlStateNormal];
   [button addTarget:target action:@selector(leftNavBtnClicked) forControlEvents:UIControlEventTouchUpInside];
   UIBarButtonItem *barButton = [[UIBarButtonItem alloc]initWithCustomView:button];

   if(SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"7"))
   {
     UIBarButtonItem *negativeSpacer = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace
                                       target:nil action:nil];
     negativeSpacer.width = fixedSpace;
     return @[negativeSpacer,barButton];
   }
   else{
     return @[barButton];
   }

   return @[barButton];
}

答案 1 :(得分:0)

只需覆盖默认

即可
self.navigationItem.hidesBackButton = YES;
UIBarButtonItem *back = [[UIBarButtonItem alloc]init];
back.title = @"Pick Me";
back.image = @"Your image";
[self.navigationItem setLeftBarButtonItem:back];

答案 2 :(得分:0)

设置右栏按钮项

 self.navigationItem.leftBarButtonItem=[[UIBarButtonItem alloc]init];

    UIImage *img1=[UIImage imageNamed:@"edit"];
    CGRect frameimg1 = CGRectMake(0, 0, img1.size.width, img1.size.height);
    UIButton *signOut=[[UIButton alloc]initWithFrame:frameimg1];
    [signOut setBackgroundImage:img1 forState:UIControlStateNormal];
    [signOut addTarget:self action:@selector(btnEditClicked:)
      forControlEvents:UIControlEventTouchUpInside];
//    [signOut setShowsTouchWhenHighlighted:YES];

    UIBarButtonItem *barButton=[[UIBarButtonItem alloc]initWithCustomView:signOut];
    self.navigationItem.rightBarButtonItem=barButton;

设置左栏按钮项

  UIImage *img11=[UIImage imageNamed:@"home"];
    CGRect frameimg11 = CGRectMake(0, 0, img11.size.width, img11.size.height);
    UIButton *signOut1=[[UIButton alloc]initWithFrame:frameimg11];
    [signOut1 setBackgroundImage:img11 forState:UIControlStateNormal];
    [signOut1 addTarget:self action:@selector(showLeftMenuPressed:)
      forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *barButton1=[[UIBarButtonItem alloc]initWithCustomView:signOut1];
    self.navigationItem.leftBarButtonItem=barButton1;

    self.navigationController.navigationBar.barTintColor=ColorNav;
    self.navigationController.navigationBar.translucent=FALSE;

设置导航标题&颜色

   self.title = titletext;
   [[[self navigationController] navigationBar]setTitleTextAttributes:@{NSForegroundColorAttributeName: textColor}];

enter image description here