UINavigationBar 在 PushViewController 上隐藏 UIBarButtons

Posted

技术标签:

【中文标题】UINavigationBar 在 PushViewController 上隐藏 UIBarButtons【英文标题】:UINavigationBar hiding UIBarButtons on PushViewController 【发布时间】:2015-04-10 06:11:55 【问题描述】:

我进行了很多搜索并尝试了所有方法,但没有成功。我正在使用带有 RightBarButtonItems 数组的自定义 UINavigationBar。请查看以下代码和图像以获取更多信息 -

-(void) makeLeftBarButtonWithTarget:(id)target action:(SEL)action

   UIImageView *imageFrame = [[UIImageView alloc]initWithFrame:CGRectMake(25.0, 2.0, 40, 30)];
   imageFrame.image =[UIImage imageNamed:@"btn.png"];
   UIButton *leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
   [leftButton addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
   leftButton.frame = CGRectMake(-9.0,0.0,60.0f, 30.0f);
   UIView *buttonView = [[UIView alloc]initWithFrame:CGRectMake(0, 7, 60.0f, 30.0f)];
  [buttonView addSubview:leftButton];
  [buttonView addSubview:imageFrame];
  barButtonItem = [[UIBarButtonItem alloc] initWithCustomView:buttonView];


-(void)makeRightBarButtons

   UIImageView *imageFrame1 = [[UIImageView alloc]initWithFrame:CGRectMake(25.0, 2.0, 30, 35)];
  imageFrame1.image =[UIImage imageNamed:@"ic_notifications.png"];
  UIButton *leftButton1 = [UIButton buttonWithType:UIButtonTypeCustom];
  [leftButton1 addTarget:self action:@selector(notificationBtnClicked) forControlEvents:UIControlEventTouchUpInside];
  leftButton1.frame = CGRectMake(-9.0,0.0,60.0f, 30.0f);
  UIView *buttonView1 = [[UIView alloc]initWithFrame:CGRectMake(0, 7, 60.0f, 30.0f)];
 [buttonView1 addSubview:leftButton1];
 [buttonView1 addSubview:imageFrame1];
 notificationBarBtnItem = [[UIBarButtonItem alloc] initWithCustomView:buttonView1];


 UIImageView *imageFrame2 = [[UIImageView alloc]initWithFrame:CGRectMake(25.0, 2.0, 30, 35)];
 imageFrame2.image =[UIImage imageNamed:@"ic_add_note.png"];
 UIButton *leftButton2 = [UIButton buttonWithType:UIButtonTypeCustom];
 leftButton2.frame = CGRectMake(-9.0,0.0,60.0f, 30.0f);
 UIView *buttonView2 = [[UIView alloc]initWithFrame:CGRectMake(0, 7, 60.0f, 30.0f)];
 [buttonView2 addSubview:leftButton2];
 [buttonView2 addSubview:imageFrame2];
 noteBarBtnItem = [[UIBarButtonItem alloc] initWithCustomView:buttonView2];

UIButton *leftButton3 = [UIButton buttonWithType:UIButtonTypeCustom];
//set this name on view
[leftButton3 setTitle:@"Start Class" forState:UIControlStateNormal];
[leftButton3 addTarget:self action:@selector(startStopClassBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
leftButton3.frame = CGRectMake(25.0, 2.0,150.0f, 30.0f);
UIView *buttonView3 = [[UIView alloc]initWithFrame:CGRectMake(0, 7, 150.0f, 30.0f)];
[buttonView3 addSubview:leftButton3];
startClassBarBtnItem = [[UIBarButtonItem alloc] initWithCustomView:buttonView3];



- (void)viewDidLoad 
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
        if ([self.navigationController.parentViewController respondsToSelector:@selector(revealGesture:)] && [self.navigationController.parentViewController respondsToSelector:@selector(revealToggle:)]) 
        UIPanGestureRecognizer *navigationBarPanGestureRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self.navigationController.parentViewController action:@selector(revealGesture:)];
        [self.navigationController.navigationBar addGestureRecognizer:navigationBarPanGestureRecognizer];

        UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0,0,1024,44)];
        view.backgroundColor = [UIColor blueColor];
        [self.navigationController.navigationBar addSubview:view];
        self.title =@"First";

       customizeUI  =[[CustomizeUI alloc]init];
        [customizeUI makeLeftBarButtonWithTarget:self.navigationController.parentViewController action:@selector(revealToggle:)];
       // self.navigationItem.rightBarButtonItem = customizeUI.barButtonItem;
    
customizeUI.delegate =self;

[customizeUI makeRightBarButtons];
self.navigationItem.rightBarButtonItems = [NSArray arrayWithObjects: customizeUI.barButtonItem,customizeUI.notificationBarBtnItem,customizeUI.noteBarBtnItem,customizeUI.startClassBarBtnItem,nil];

//[[UINavigationBar appearance] setBarTintColor:[UIColor blueColor]];
UIView *statusBarView = [[UIView alloc] initWithFrame:CGRectMake(0, -20, 1024, 22)];
statusBarView.backgroundColor = [UIColor blueColor];
// [self.navigationController.navigationBar addSubview:statusBarView];


-(void)viewWillAppear:(BOOL)animated

   self.navigationItem.hidesBackButton = NO;
   self.navigationController.navigationBarHidden = NO;

我将这段代码放在两个视图控制器上,但仍然无法正常工作。当我在第一个视图控制器上运行代码时,所有按钮都正确显示,但是当我调用推送导航并移动到下一个视图控制器时,它没有显示按钮,甚至后退按钮也不起作用,我在第二个视图控制器上添加了一个后退按钮使用当我导航到第一个视图控制器时,那个后退按钮在没有颜色的标题上没有显示任何内容。请帮我解决这个问题。

【问题讨论】:

在 viewdidload 上隐藏您的默认导航栏 @Aju: 非常感谢..iT 工作正常.. :) 欢迎,我把我的评论作为答案,它可能对其他人有所帮助。 【参考方案1】:

问题是因为您的默认导航正在过渡到您的导航,因此您在导航栏中添加的控件将被隐藏。根据要解决的评论,您需要隐藏默认导航栏。

【讨论】:

以上是关于UINavigationBar 在 PushViewController 上隐藏 UIBarButtons的主要内容,如果未能解决你的问题,请参考以下文章

UINavigationBar

在 UINavigationController 子类中设置 UINavigationBar 子类

UIBarButton 未在 UINavigationBar 中显示

UITableView 滚动 UINavigationBar

UINavigationBar 标题文本部分显示在屏幕上

UINavigationBar 在推送时更改颜色