UISegmentedControl embedded in a UINavigationBar/Item

前端 未结 1 1067
孤街浪徒
孤街浪徒 2020-12-24 09:38

I would like to embed a UISegmentedControl somewhere in my UINavigationControllers topbar.

It is no problem embedding it in a UIBarBu

相关标签:
1条回答
  • 2020-12-24 10:13

    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:

    alt text alt text

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

    0 讨论(0)
提交回复
热议问题