0

I used Navigation Controller & navigation Bar in StroryBoard.

I want to customize navigation Bar for one view as follow for right side of navigation bar.

enter image description here

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?

Community
  • 1
  • 1
user2893370
  • 691
  • 8
  • 22

2 Answers2

1

Just use Segmented Control: you have a full example HERE

dmerlea
  • 826
  • 4
  • 9
  • but in Segment control I am not able to set custom image.:( – user2893370 Sep 19 '14 at 07:07
  • @user2893370 No, you can custom SegmentedControl check http://stackoverflow.com/questions/21603822/ios-6-uisegmentedcontrol-with-ios-7-design and http://stackoverflow.com/questions/18900034/use-ios-6-style-segmented-control-in-ios-7 – Huy Nghia Sep 19 '14 at 07:22
1

declare a segment control with custom view

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

and then add it to barbuttonitem view

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.

sreekanthk
  • 361
  • 2
  • 21