UISegmentedControl嵌入在UINavigationBar/Item中

Ric*_*kiG 8 uisegmentedcontrol uinavigationitem ipad ios

我想UISegmentedControl在我UINavigationController的topbar中嵌入一​​个地方.

将其嵌入并将其UIBarButtonItem设置为左或右barButtonItem 是没有问题的.

在处理iPhone的屏幕空间时,我可以理解这种方法.但是,我在iPad上的Popover中执行此操作,并且顶部栏中有很多可用的垂直空间.如果我将segmentedControl添加为左或右barButtonItem,它会缩小,以便我看不到我的段按钮上的文本,它会变成"完成"按钮等的宽度.如果我尝试将其添加到navigationItem TitleView它将一直向右显示,并且仍然缩小比我的3段控件更多,文本可以显示.

我将如何添加UISegmentedControlUINavigationController包含我的弹出窗口内容的中心.

希望有人可以帮助我:)提前感谢.

Cos*_*que 21

为什么需要将控件放在popover标题栏中?iPad有更多的屏幕空间可以考虑将其放入下面的视图中.

- 编辑 -

我自己试了一下它的确有效.以下是设置popover控制器的代码:

- (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];
}
Run Code Online (Sandbox Code Playgroud)

这是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;
}
Run Code Online (Sandbox Code Playgroud)

结果如下:

替代文字 替代文字

除了发送sizeToFit到分段控件之外,我的代码中没有技巧.这对你有用吗?

  • 现在就是服务.我只是希望下次我能来一些代码时Costique会注意并回答这么简洁的解决方案.谢谢. (2认同)