UISegmentedControl built into UINavigationBar / Item

I would like to insert a UISegmentedControl somewhere in the top panel of the UINavigationController .

You should not embed it in UIBarButtonItem and set it as left or right barButtonItem.

I can understand this approach when dealing with iPhone screen real estate. However, I do this in Popover on the iPad, and there is β€œa lot” of vertical space in the top bar. If I add segmentedControl as left or right barButtonItem, it will be reduced so that I can not see the text on my segment button, it becomes the width of the Finish button, etc. If I try to add it to the navigationItem, the TitleView will be displayed all the way to the right and is still shrinking more than my 3-segment control with text can be displayed.

How can I add a UISegmentedControl to the center of the UINavigationController that wraps my popovers content.

Hope someone can help me :) thanks in advance.

+8
ios ipad uinavigationitem uisegmentedcontrol
source share
1 answer

Why do you need to put the control in the popover title bar? The iPad has much more screen real estate to consider including in the view below.

- EDIT -

I tried it myself and it works. Here is the code installing 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:

alt textalt text

There are no tricks in my code except sending sizeToFit to the segmented control. Does this work for you?

+21
source share

Source: https://habr.com/ru/post/650055/


All Articles