问题
I would like to embed a UISegmentedControl
somewhere in my UINavigationController
s topbar.
It is no problem embedding it in a UIBarButtonItem
and setting it as the left or right barButtonItem.
I can understand this approach when dealing with the screen real-estate of an iPhone. I am, however, doing this in a Popover on an iPad and there is "lots" of vertical space available in the topbar. If I add the segmentedControl as a left or right barButtonItem it gets scaled down so that I can not see the text on my segment button, it gets to be the width of a "Done" button etc. If I try to add it to the navigationItem TitleView it will show up all the way to the right and still scaled down more than my 3 segment control with text can display.
How would I go about adding a UISegmentedControl
to the center of the UINavigationController
that wraps my popovers content.
Hope someone can help me out:) thanks in advance.
回答1:
Why would you need to put the control in the popover title bar? iPad has much more screen real estate to consider putting it into the view below.
-- EDIT --
I tried it myself and it works. Here is the code setting up the popover controller:
- (IBAction) showPopover: (id) sender
{
TestController *testController = [[TestController alloc] initWithStyle: UITableViewStylePlain];
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController: testController];
UIPopoverController *controller = [[UIPopoverController alloc] initWithContentViewController: navController];
[controller presentPopoverFromBarButtonItem: sender permittedArrowDirections: UIPopoverArrowDirectionAny animated: YES];
controller.delegate = self;
[testController release];
[navController release];
}
Here is the implementation of TestController:
- (id) initWithStyle: (UITableViewStyle) style
{
if ( (self = [super initWithStyle: style]) ) {
UISegmentedControl *ctrl = [[UISegmentedControl alloc] initWithFrame: CGRectZero];
ctrl.segmentedControlStyle = UISegmentedControlStyleBar;
[ctrl insertSegmentWithTitle: @"One" atIndex: 0 animated: NO];
[ctrl insertSegmentWithTitle: @"Two" atIndex: 0 animated: NO];
[ctrl insertSegmentWithTitle: @"Three" atIndex: 0 animated: NO];
[ctrl sizeToFit];
// Any of the following produces the expected result:
self.navigationItem.titleView = ctrl;
//self.navigationItem.leftBarButtonItem = [[[UIBarButtonItem alloc] initWithCustomView: ctrl] autorelease];
[ctrl release];
}
return self;
}
Here is the result:


There are no tricks in my code besides sending sizeToFit
to the segmented control. Does this work for you?
来源:https://stackoverflow.com/questions/4417673/uisegmentedcontrol-embedded-in-a-uinavigationbar-item