简体   繁体   English

在ios中自定义导航栏

[英]Customize Navigation Bar in ios

I used Navigation Controller & navigation Bar in StroryBoard . 我在StroryBoard中使用了导航控制器和导航栏。

I want to customize navigation Bar for one view as follow for right side of navigation bar. 我想为一个视图自定义导航栏,如下所示。

在此处输入图片说明

My tried code 我试过的代码

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

Problem of tried code: There is a space between two buttons. 尝试的代码的问题:两个按钮之间有一个空格。

How can I achieve this? 我该如何实现?

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

declare a segment control with custom view 使用自定义视图声明段控件

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

and then add it to barbuttonitem view 然后将其添加到barbuttonitem视图

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

dont forget to write the lines that relates to segment control customized view like ur requirement adding red or black images to the segment 0 or 1. 不要忘记写与段控制自定义视图相关的行,例如您的要求向段0或1添加红色或黑色图像。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM