Crystal
Crystal

Reputation: 29458

How to center UISegmentControl in a toolbar at top of UIPopoverController

I've seen a few different posts on this, but I can't seem to get it working. I basically have a UITableView and want sort buttons at the top of a popover controller. I followed this post: UIPopoverController toolbar at top in order to get started. In my controller that is the rootViewController of the navigationController, I can create a UISegmentControl and place it at the top. However, it does not look like the picture in that it's not centered. Maybe because the way I get it into the popover is in the viewDidLoad of the popover like this:

   UISegmentedControl *topSegmentControl = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:@"One", @"Two", @"Three", @"Four", nil]];
    topSegmentControl.backgroundColor = [UIColor clearColor];
    topSegmentControl.segmentedControlStyle = UISegmentedControlStyleBar;
    UIBarButtonItem *toolBarCustom = [[UIBarButtonItem alloc] initWithCustomView:topSegmentControl];
//    UIBarButtonItem *spaceItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
    self.navigationItem.leftBarButtonItem = toolBarCustom;

Also, if I want to present data at the bottom of the popovercontroller in a toolbar, I'm not sure where to do that. Following the same example: UIPopoverController toolbar at top, I thought in my navigationController, I would do something like this:

 UISegmentedControl *topSegmentControl = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:@"BottomOne", @"BottomTwo", @"BottomThree", nil]];
//    topSegmentControl.backgroundColor = [UIColor clearColor];
    topSegmentControl.segmentedControlStyle = UISegmentedControlStyleBar;
    UIBarButtonItem *toolBarCustom = [[UIBarButtonItem alloc] initWithCustomView:topSegmentControl];
    UIBarButtonItem *spaceItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
    NSArray *array = [NSArray arrayWithObjects:spaceItem, toolBarCustom, spaceItem, nil];
    [navController setToolbarItems:toolBarCustom];
    [navController setToolbarHidden:NO];

When I try this, I see a toolBar with nothing in it, that is a lighter tint than the rest of the popover.

To summarize, I'm not sure as to where you would initialize toolbar or barbuttonitems for a popover that has a navigationcontroller like in the example. I'm also not sure how to center the data. Thanks.

Upvotes: 1

Views: 381

Answers (1)

Noah Witherspoon
Noah Witherspoon

Reputation: 57149

You’ll probably be best suited by just setting your UISegmentedControl as the titleView of your root view controller’s navigationItem.

Upvotes: 2

Related Questions