如何使顶级导航控制器可点击?

时间:2018-03-07 09:23:25

标签: ios objective-c uiview uinavigationcontroller uibutton

我有以下故事板。storyboard导航到类VC,我需要在VC顶部创建自定义按钮,我认为它将位于顶部导航栏控制器下方。如何在导航栏控制器顶部创建自定义按钮并单击。我已经将顶部导航栏外观设为透明。

我有以下代码正常工作,布局如下图pic但是这不是我想要的布局。它位于透明导航栏控制器区域下方的某处。

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

}

我设法通过更改Y坐标将自定义按钮移到顶部,我得到了以下结果。它不是理想的原因我需要棕色一直到手机的边缘,特别是iPhoneX和可点击的按钮。 Pic 2

2 个答案:

答案 0 :(得分:0)

我无法评论这就是为什么建议这样做的答案。

我认为你应该展示你的视图控制器,而不是推动它。

[self presentViewController:@"your view controller" animated:YES completion:nil];

答案 1 :(得分:0)

答案:

隐藏当前VC或第一个VC的导航栏

-(void)viewWillAppear:(BOOL)animated{

    [[self navigationController] setNavigationBarHidden:YES animated:YES];

}

取消隐藏后续VC或第二个VC上的导航栏。在第一个VC,应用以下代码。

-(void)viewDidDisappear:(BOOL)animated{

   [[self navigationController] setNavigationBarHidden:NO animated:YES];
}

然后按钮将是可点击的。