UINavigationBar标题内的UISegmentedControl看起来没有格式化

时间:2013-03-23 14:37:28

标签: iphone uinavigationcontroller uinavigationbar uisegmentedcontrol

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

enter image description here

当我希望它看起来像这样(漂亮:)。任何人都可以帮忙??

enter image description here

我读过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;

2 个答案:

答案 0 :(得分:0)

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

UISegmentedControlStylePlain
UISegmentedControlStyleBordered
UISegmentedControlStyleBar
UISegmentedControlStyleBezeled

例如:

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

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

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

答案 1 :(得分:0)

是的,您需要在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];

会留下这样的事情:

UISegmented Control

显然,你也可以做很多其他的定制工作。请查看此处的文档:http://developer.apple.com/library/ios/#documentation/uikit/reference/UISegmentedControl_Class/Reference/UISegmentedControl.html