UINavigationBar在PushViewController上隐藏UIBarButtons

时间:2015-04-10 06:11:56

标签: ios objective-c iphone uinavigationcontroller

我搜索了很多并尝试了一切,但它没有用。我正在使用带有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;
}

enter image description here

enter image description here

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

1 个答案:

答案 0 :(得分:0)

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