简体   繁体   中英

UIPopover: UISegmentedControl instead of title

I have a Popover and the title @"Title". Is it possible to have UISegmentedControl there, instead of the title? In other words, I want a popover which has a segmentedcontrol in the middle of header. How to?

The content of popover is UITableViewController, it is inside a UINavigationController, and I present it via presentPopoverFromBarButtonItem:.

Use a UIToolbar at the top of the popover view. Either in interface builder, or programatically, insert a UISegmentedControl with flexible space either side of it. For example:

UIBarButtonItem *flexibleSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace
                                                                                   target:nil
                                                                                   action:nil];

UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems
                                        [NSArray arrayWithObjects:@"Segment 1", @"Segment 2", nil]];
UIBarButtonItem *segmentedBarButton = [[UIBarButtonItem alloc] initWithCustomView:segmentedControl];

[self.toolbar setItems:[NSArray arrayWithObjects:flexibleSpace, segmentedBarButton, flexibleSpace]];
UISegmentedControl *sc = [[[UISegmentedControl alloc] initWithItems:nil] autorelease];
// config the control

UIBarButtonItem *scItem = [[[UIBarButtonItem alloc] initWithCustomView:sc] autorelease];

// add the segmented control, instead of the UILabel title
self.navigationItem.titleView = scItem.customView;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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