Search code examples
iosobjective-cuiviewuinavigationcontrolleruibutton

How to make top navigation controller clickable?


I have the following storyboard.storyboard Navigating to Class VC, I need to create custom buttons at the top of the VC and I think it will be beneath the Top Navigation Bar Controller. How do I make my custom buttons on top of the Navigation Bar Controller and clickable. I have already make the Top Navigation Bar appearance to be transparent.

I have the following code that is working and the layout is at the following pic pic However this is not my desired layout. It is located somewhere below the Transparent Navigation Bar controller area.

 //--- customizeable button attributes
 CGFloat X_BUFFER = 0.0; //--- the number of pixels on either side of the segment
 CGFloat Y_BUFFER = 0.0; //--- number of pixels on top of the segment
 CGFloat HEIGHT = 50.0; //--- height of the segment

 //--- customizeable selector bar attributes (the black bar under the buttons)
 CGFloat SELECTOR_Y_BUFFER = 0.0; //--- the y-value of the bar that shows what page you are on (0 is the top)
 CGFloat SELECTOR_HEIGHT = 44.0; //--- thickness of the selector bar

 CGFloat X_OFFSET = 8.0; //--- for some reason there's a little bit of a glitchy offset.  I'm going to look for a better workaround in the future

 CGFloat numControllers = 7.0;

 -(void)setupSegmentButtons
{
navigationView = [[UIView alloc]initWithFrame:CGRectMake(0,0,self.view.frame.size.width,52)];
navigationView.backgroundColor = [UIColor greenColor];
[self.view addSubview:navigationView];

//——Format Some Dates

for (int i = 0; i<numControllers; i++) {


    UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(i*(self.view.frame.size.width/numControllers), Y_BUFFER, (self.view.frame.size.width/numControllers),HEIGHT)];

    [navigationView addSubview:button];

    NSString *lblDate = [datelblFormat stringFromDate:[calendar dateFromComponents:comps]];

    UILabel *firstLineButton = [[UILabel alloc] initWithFrame:CGRectMake(-4,0,self.view.frame.size.width/numControllers,30)];

    firstLineButton.text = lblDate;

    [button addSubview:firstLineButton];

    NSString *lblDay = [daylblFormat stringFromDate:[calendar dateFromComponents:comps]];

    UILabel *secondLineButton = [[UILabel alloc] initWithFrame:CGRectMake(-4,30,self.view.frame.size.width/numControllers,15)];

    secondLineButton.text = lblDay;

    [button addSubview:secondLineButton];

    button.tag = i; //--- IMPORTANT: if you make your own custom buttons, you have to tag them appropriately

    button.backgroundColor = [UIColor brownColor];//— buttoncolors

    [button addTarget:self action:@selector(tapSegmentButtonAction:) forControlEvents:UIControlEventTouchUpInside];

    ++comps.day;
}

[self setupSelector];
}

//--- sets up the selection bar under the buttons on the navigation bar
-(void)setupSelector {
    selectionBar = [[UIView alloc]initWithFrame:CGRectMake(self.view.frame.size.width/numControllers, Y_BUFFER, (self.view.frame.size.width/numControllers),HEIGHT)];
    selectionBar.backgroundColor = [UIColor colorWithRed:189.0f/255.0f green:225.0f/255.0f blue:255.0f/255.0f alpha:0.6];    
    [navigationView addSubview:selectionBar];

}

I manage to move the custom buttons to the top by changing the Y Coordinates and I got the following results. Its not ideal cause I need the brown colour to be all the way to the edge of the phone especially iPhoneX and the buttons to be clickable. Pic 2


Solution

  • Answer:

    To hide the navigation bar at the current VC or the 1st VC

    -(void)viewWillAppear:(BOOL)animated{
    
        [[self navigationController] setNavigationBarHidden:YES animated:YES];
    
    }
    

    To UNHIDE the navigation bar on subsequent VC or the 2nd VC. At the 1st VC, apply the following code.

    -(void)viewDidDisappear:(BOOL)animated{
    
       [[self navigationController] setNavigationBarHidden:NO animated:YES];
    }
    

    Then the buttons will be clickable.