【问题标题】:How to make top navigation controller clickable?如何使顶部导航控制器可点击?
【发布时间】:2018-08-15 08:06:51
【问题描述】:

我有以下情节提要。 导航到 VC 类,我需要在 VC 顶部创建自定义按钮,我认为它将位于顶部导航栏控制器下方。如何使我的自定义按钮位于导航栏控制器的顶部并且可点击。我已经使顶部导航栏外观透明。

我有以下代码正在运行,布局在下面的图片 但这不是我想要的布局。它位于透明导航栏控制器区域下方的某个位置。

 //--- 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 和可点击的按钮。

【问题讨论】:

  • 在标题路径中添加一个集合视图。
  • 对不起,我是初学者,你能给我举几个例子吗?
  • 据我了解这个问题,您希望棕色日期部分可点击吗?而且状态栏和旅游棕收藏部分应该是一样的吧?
  • 是的,我想要可点击的棕色日期。而他的棕色会一直延伸到屏幕的边缘。状态栏仍然可见。

标签: ios objective-c uiview uinavigationcontroller uibutton


【解决方案1】:

我无法发表评论,因此建议将此作为答案。

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

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

【讨论】:

  • 如果我以模态方式呈现,我的后续 VC 中将不再有后退按钮。我需要返回按钮。
  • 您可以在屏幕上的某处添加完成按钮或关闭按钮,并在该操作上关闭视图控制器。
【解决方案2】:

答案:

隐藏当前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];
}

然后按钮将是可点击的。

【讨论】:

    猜你喜欢
    • 2016-06-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-06
    • 2011-07-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多