8

我想UISegmentedControl在我UINavigationController的顶部栏中嵌入一个。

将其嵌入到a中UIBarButtonItem并将其设置为左侧或右侧barButtonItem是没有问题的。

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

我将如何在包装我的弹出框内容UISegmentedControl的中心添加一个。UINavigationController

希望有人可以帮助我:) 在此先感谢。

4

1 回答 1

21

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

- 编辑 -

我自己尝试过,它有效。这是设置弹出框控制器的代码:

- (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];
}

下面是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;
}

结果如下:

替代文字 替代文字

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

于 2010-12-11T16:40:38.763 回答