【问题标题】:How to center UISegmentControl in a toolbar at top of UIPopoverController如何在 UIPopoverController 顶部的工具栏中将分段控件居中
【发布时间】:2011-08-01 18:36:44
【问题描述】:

我已经看到了一些不同的帖子,但我似乎无法让它发挥作用。我基本上有一个 UITableView 并且想要在弹出控制器顶部的排序按钮。我关注了这篇文章:UIPopoverController toolbar at top 以便开始。在作为导航控制器的 rootViewController 的控制器中,我可以创建一个 UISegmentControl 并将其放在顶部。但是,它看起来不像图片,因为它没有居中。也许是因为我将它放入弹出框的方式是在弹出框的 viewDidLoad 中,如下所示:

   UISegmentedControl *topSegmentControl = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:@"One", @"Two", @"Three", @"Four", nil]];
    topSegmentControl.backgroundColor = [UIColor clearColor];
    topSegmentControl.segmentedControlStyle = UISegmentedControlStyleBar;
    UIBarButtonItem *toolBarCustom = [[UIBarButtonItem alloc] initWithCustomView:topSegmentControl];
//    UIBarButtonItem *spaceItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
    self.navigationItem.leftBarButtonItem = toolBarCustom;

另外,如果我想在工具栏中的 popovercontroller 底部显示数据,我不知道在哪里做。按照同样的例子:UIPopoverController toolbar at top,我想在我的导航控制器中,我会做这样的事情:

 UISegmentedControl *topSegmentControl = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:@"BottomOne", @"BottomTwo", @"BottomThree", nil]];
//    topSegmentControl.backgroundColor = [UIColor clearColor];
    topSegmentControl.segmentedControlStyle = UISegmentedControlStyleBar;
    UIBarButtonItem *toolBarCustom = [[UIBarButtonItem alloc] initWithCustomView:topSegmentControl];
    UIBarButtonItem *spaceItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
    NSArray *array = [NSArray arrayWithObjects:spaceItem, toolBarCustom, spaceItem, nil];
    [navController setToolbarItems:toolBarCustom];
    [navController setToolbarHidden:NO];

当我尝试这个时,我看到一个工具栏没有任何内容,它比弹出框的其余部分颜色更浅。

总而言之,我不确定您将在哪里初始化工具栏或 barbuttonitems 以用于具有示例中导航控制器的弹出框。我也不确定如何使数据居中。谢谢。

【问题讨论】:

    标签: iphone uinavigationcontroller uipopovercontroller uibarbuttonitem uitoolbar


    【解决方案1】:

    您可能最适合将 UISegmentedControl 设置为根视图控制器的 navigationItemtitleView

    【讨论】:

    • 但是 UIBarButtonItem 不是 UIView 的子项。
    • 你不需要为它创建一个 UIBarButtonItem。从字面上看,只需设置self.navigationItem.titleView = topSegmentControl
    猜你喜欢
    • 1970-01-01
    • 2011-09-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-19
    • 1970-01-01
    相关资源
    最近更新 更多