【问题标题】:Customize Navigation Bar in ios在ios中自定义导航栏
【发布时间】:2014-09-19 06:58:05
【问题描述】:

我在故事板中使用了导航控制器和导航栏。

我想为一个视图自定义导航栏,如下导航栏右侧。

我尝试过的代码

    UIButton *btn_list=[UIButton buttonWithType:UIButtonTypeCustom];
    btn_list.frame=CGRectMake(0, 0, 60, 30);
    [btn_list setTitle:@"Liste" forState:UIControlStateNormal];
    [btn_list setBackgroundImage:[UIImage imageNamed:@"red-left.png"] forState:UIControlStateNormal];
    [btn_list setBackgroundImage:[UIImage imageNamed:@"black-left.png"] forState:UIControlStateSelected];
    UIBarButtonItem *list_bar=[[UIBarButtonItem alloc]initWithCustomView:btn_list];
    
    UIButton *btn_map=[UIButton buttonWithType:UIButtonTypeCustom];
    btn_map.frame=CGRectMake(0, 0, 60, 30);
    [btn_map setTitle:@"Karte" forState:UIControlStateNormal];
    [btn_map setBackgroundImage:[UIImage imageNamed:@"red-right.png"] forState:UIControlStateNormal];
    [btn_list setBackgroundImage:[UIImage imageNamed:@"black-right.png"] forState:UIControlStateSelected];
    UIBarButtonItem *map_bar=[[UIBarButtonItem alloc]initWithCustomView:btn_map];

    self.navigationItem.rightBarButtonItems=[[NSArray alloc]initWithObjects:list_bar,map_bar, nil];

试过代码的问题:两个按钮之间有空格。

我怎样才能做到这一点?

【问题讨论】:

  • 请澄清您想将段控件用作条形按钮项。我说的对吗?
  • @sreekanthk,是的,你是对的
  • 然后你使用两个按钮而不是使用 UISegmentControl
  • 但在分段控制中我无法设置自定义图像。:(
  • 缩小你的图片大小,

标签: ios objective-c iphone xcode uinavigationcontroller


【解决方案1】:

只使用分段控制:你有一个完整的例子HERE

【讨论】:

【解决方案2】:

用自定义视图声明一个段控件

UISegmentedControl *control = (UISegmentedControl *) [segmentBarButton customView];

然后将其添加到 barbuttonitem 视图中

UIBarButtonItem *segmentBarButton=[[UIBarButtonItem alloc] initWithCustomView:control];

不要忘记编写与段控制自定义视图相关的行,例如您的要求将红色或黑色图像添加到段 0 或 1。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-09-04
    • 2011-12-09
    • 2015-08-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-07
    相关资源
    最近更新 更多