【问题标题】:UISegmentedControl inside title of UINavigationBar looks unformattedUINavigationBar 标题内的 UISegmentedControl 看起来未格式化
【发布时间】:2013-03-13 08:24:56
【问题描述】:

我正在尝试在 UINavigationController 的标题中添加 UISegmentedControl。但是,格式看起来像这样(即丑陋)。

当我希望它看起来像这样时(漂亮:)。谁能帮忙??

我读过 Red Artisan here 的流行示例。但是我并没有将其作为我的第一个视图(就像 Red Artisan 那样),所以我已经将很多代码从 App Delegate 中移出。在 App Delegate 中,我确实将此屏幕设置为 UINavigationController,其 rootView 为 UIViewController。

GenInfoViewController *genInfoController = [[GenInfoViewController alloc] initWithNibName:@"GenInfoViewController" bundle:nil];

UINavigationController *genInfoNavController = [[UINavigationController alloc] initWithRootViewController:genInfoController];

然后在 GenInfoViewController.m 的 viewDidLoad 中执行以下操作:

self.segmentedControl = [[UISegmentedControl alloc] initWithItems:@[@"Info",@"Map"]];
self.navigationItem.titleView = self.segmentedControl;

【问题讨论】:

    标签: iphone uinavigationcontroller uinavigationbar uisegmentedcontrol


    【解决方案1】:

    要设置分段控件的样式,请将segmentedControlStyle 属性设置为以下之一:

    UISegmentedControlStylePlain
    UISegmentedControlStyleBordered
    UISegmentedControlStyleBar
    UISegmentedControlStyleBezeled
    

    例如:

    self.segmentedControl = [[UISegmentedControl alloc] initWithItems:@[@"Info",@"Map"]];
    self.segmentedControl.segmentedControlStyle = UISegmentedControlStyleBordered;
    self.navigationItem.titleView = self.segmentedControl;
    

    这里有一些关于样式段控件的相关 Q+As:

    如果您想尝试自定义分段控件,请查看所有可用的 CocoaControlsCocoaPods

    【讨论】:

    • segmentedControlStyle 对于 iOS7+ 已弃用。
    【解决方案2】:

    是的,您需要在 UISegmented 控件上设置属性“segmentedControlStyle”。

    您的选择如下:

    typedef enum {
       UISegmentedControlStylePlain,
       UISegmentedControlStyleBordered,
       UISegmentedControlStyleBar, // This is probably the one you want!
       UISegmentedControlStyleBezeled,
    } UISegmentedControlStyle;
    

    所以下面的方法应该可以解决问题:

    self.segmentedControl = [[UISegmentedControl alloc] initWithItems:@[@"Info",@"Map"]];
    self.segmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;
    self.navigationItem.titleView = self.segmentedControl;
    

    您可能还需要考虑设置分段控件的“tintColor”。

    self.segmentedControl = [UIColor blackColour];
    

    会给你留下这样的东西:

    显然,您还可以进行许多其他自定义操作。看看这里的文档:http://developer.apple.com/library/ios/#documentation/uikit/reference/UISegmentedControl_Class/Reference/UISegmentedControl.html

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-09-29
      • 1970-01-01
      • 2012-08-02
      • 2021-05-14
      • 2011-04-09
      相关资源
      最近更新 更多