Jay
Jay

Reputation: 299

Adding a segmented control to the center of UINavigationBar

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.

Upvotes: 3

Views: 4276

Answers (3)

jalopezsuarez
jalopezsuarez

Reputation: 434

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.

Upvotes: 1

junaidsidhu
junaidsidhu

Reputation: 3580

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;

Upvotes: 5

Rajneesh071
Rajneesh071

Reputation: 31083

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

Upvotes: 3

Related Questions