3

I am trying to add a UISegmentedControl to the middle of a UINavigationBar of only one view (not the entire view controller). How can I go about doing this?

Other answers I read only allow an entire view controller to contain a UINavigationItem as the title. I need it to show only on one view.

Jay
  • 299
  • 4
  • 13
  • 1
    follow my answer http://stackoverflow.com/questions/13890380/addimg-mutiple-button-on-navigation-bar-in-iphone-sdk/13890404#13890404 – Rajneesh071 Dec 30 '12 at 11:17
  • http://stackoverflow.com/questions/13565962/how-to-programmetically-add-navigation-bar-and-back-button-on-it/13619760#13619760 – Rajneesh071 Dec 30 '12 at 11:21
  • @Rajneesh071 Your answer from the other questions did not answer my question. Or are you suggesting to me to draw a view over the navigation bar? – Jay Dec 30 '12 at 13:34
  • yes i am suggesting you to add sub view over navigation – Rajneesh071 Dec 30 '12 at 13:53
  • @Rajneesh071 Is that the only way? – Jay Dec 30 '12 at 14:02
  • title view....is the other way – Rajneesh071 Dec 30 '12 at 14:45
  • @Rajneesh071 The app crashes when I do the following: `NSArray *arrayOfItems = [[NSArray alloc] initWithObjects:@"A",@"B",@"C", nil];` `UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems:arrayOfItems];` `self.navigationController.navigationItem.titleView = segmentedControl;` – Jay Dec 30 '12 at 15:36
  • you can add sub view..and according to your query.."To the center of nav" you can add as subView, just chk ma answer – Rajneesh071 Dec 31 '12 at 13:22
  • hey check my answer...its according to your question, mean adding in middle of navigationBar – Rajneesh071 Jan 07 '13 at 13:39
  • I'm really grateful for your attempt to help me with this, but JayD's code explained it better for me. I only had to change 1 line of his code to make it work for my purpose. – Jay Jan 08 '13 at 12:21
  • ok dear..enjoy...but your question was related to adding in the middle..thats y i suggest you the answer...:) – Rajneesh071 Jan 08 '13 at 13:08

3 Answers3

5

This code will help you.

UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems:
                                            [NSArray arrayWithObjects:@"Add",@"Delete",
                                             nil]];
    segmentedControl.frame = CGRectMake(0, 0, 80, 30);
    segmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;
    [segmentedControl setWidth:35.0 forSegmentAtIndex:0];
    [segmentedControl setWidth:45.0 forSegmentAtIndex:1];

    [segmentedControl addTarget:self action:@selector(segmentAction:) forControlEvents:UIControlEventValueChanged];
    segmentedControl.momentary = YES;

    UIBarButtonItem *segmentBarItem = [[UIBarButtonItem alloc] initWithCustomView:segmentedControl];
    [segmentedControl release];

    self.navigationItem.leftBarButtonItem = segmentBarItem;
    [segmentBarItem release];

result of this code is

enter image description here

Edit:

Exact code that would work:

UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems:
                                            [NSArray arrayWithObjects:@"Add",@"Delete",
                                             nil]];
    segmentedControl.frame = CGRectMake(0, 0, 80, 30);
    segmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;
    [segmentedControl setWidth:35.0 forSegmentAtIndex:0];
    [segmentedControl setWidth:45.0 forSegmentAtIndex:1];

    [segmentedControl addTarget:self action:@selector(segmentAction:) forControlEvents:UIControlEventValueChanged];
    segmentedControl.momentary = YES;

    self.navigationItem.titleView = segmentedControl;
Jay
  • 299
  • 4
  • 13
junaidsidhu
  • 3,539
  • 1
  • 27
  • 49
  • I just used `self.navigationItem.leftBarButtonItem = segmentControl;` instead and it worked. Thanks! I'll edit in the code that worked for me. – Jay Dec 31 '12 at 10:00
3

You can add your UISegmentedControl as SubView to your navigationBar.

NSArray *arrayOfItems = [[NSArray alloc] initWithObjects:@"A",@"B",@"C", nil];
UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems:arrayOfItems];
segmentedControl.frame=CGRectMake(60, 0, 200, 44);
[self.navigationController.navigationBar addSubview:segmentedControl];
Rajneesh071
  • 30,846
  • 15
  • 61
  • 74
1

If you want to add a UISegmentedControl centered on any view, and not only UINavigationView:

UISegmentedControl *segmentedTab = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:@"a", @"b", @"c", nil]];
segmentedTab.center = CGPointMake(segmentedView.frame.size.width / 2, segmentedView.frame.size.height / 2);
[self.segmentedView addSubview:segmentedTab];

Where segmentedView is a view that will contains our UISegmentedControl.

jalopezsuarez
  • 414
  • 4
  • 13