【问题标题】:backBarButtonItem not getting displayedbackBarButtonItem 未显示
【发布时间】:2015-11-02 13:04:14
【问题描述】:

我知道viewcontroller's 导航项的backBarButtonItem 会在另一个视图控制器被压入堆栈时显示,这是从顶部开始的第二个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。我没有在这些导航项中设置leftBarButtonItemleftBarButtonItemsrightBarButtonItemrightBarButtonItems

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

【问题讨论】:

  • 你添加自定义栏按钮试试看我认为它在你的问题中工作。
  • 我已经阅读过它。在 B 上设置 leftbaritems 有效。但我认为在 A 上设置 backBarButtonItem 是正确的做法。文档中也提到了它,但在我的情况下不起作用。

标签: ios objective-c iphone uinavigationcontroller uinavigationbar


【解决方案1】:

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

 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];
}

【讨论】:

    【解决方案2】:

    只需覆盖默认值

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

    【讨论】:

      【解决方案3】:

      设置右栏按钮项

       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}];
      

      【讨论】:

      • 谢谢! leftBarButtonItem 有效。我想问一下backBarButtonItem是否有bug或者我对它的工作方式有一些误解?
      • @hridayesh,如果您满意,请点赞回答。
      • @hridayesh,实际上我不知道更完美的答案,但尝试设置 nil,然后再次分配初始化它。重新设置,应该可以了。
      • 谢谢。我想为某些控制器设计导航栏。我应该在 viewwillappear 上更改全局栏的样式并在 viewwill 上重置消失吗?还是有一些更好的方法可以让我在视图控制器中定义样式而不需要重置为默认值?
      • 我尝试设置 nil 然后 alloc init 但它不起作用,我继续看到默认按钮
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-07-18
      • 2022-01-18
      • 2016-02-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多