使用"后退"按钮在导航栏中进行UISegmentedControl

Isu*_*uru 15 objective-c uinavigationbar uisegmentedcontrol uinavigationcontroller ios

我正在UISegmentedControl以编程方式将导航栏添加到应用的位置titleView.但正如Apple文档提到的那样titleView,如果leftBarButtonItem不是nil,则忽略此属性.

但我也想要后退按钮.就像他们在自己的图像中说明的那样!

在此输入图像描述

下面是我添加的代码UISegmentedControl.

self.navigationItem.leftBarButtonItem = nil;
UISegmentedControl *statFilter = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:@"Filter_Personnal", @"Filter_Department", @"Filter_Company", nil];
[statFilter setSegmentedControlStyle:UISegmentedControlStyleBar];
self.navigationItem.titleView = statFilter;
Run Code Online (Sandbox Code Playgroud)

还有另一种方法可以添加一个UISegmentedControl后退按钮吗?

谢谢.

aru*_*n.s 23

试试这个

删除这一行---> self.navigationItem.leftBarButtonItem = nil;

添加此项

UISegmentedControl *statFilter = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:@"Filter_Personnal", @"Filter_Department", @"Filter_Company", nil]];
[statFilter setSegmentedControlStyle:UISegmentedControlStyleBar];
[statFilter sizeToFit];
self.navigationItem.titleView = statFilter;
Run Code Online (Sandbox Code Playgroud)

只有改变是我添加了这一行:

[statFilter sizeToFit];
Run Code Online (Sandbox Code Playgroud)

希望这可以帮助 !!!

  • `setegmentedControlStyle`从iOS7开始被弃用...你能更新你的答案吗? (9认同)

Cap*_*uff 3

您可以创建一个UIBarButtonItem带有自定义视图的视图,该视图可能是您的UISegmentedControl.

以下内容可能会起作用。

//create segmented control with items
UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:@"One", @"Two", nil]];

//create bar button item with segmented control as custom view
UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc] initWithCustomView:segmentedControl];

//add segmented control bar button item to navigation bar
[[[self navigationController] navigationItem] setRightBarButtonItem:barButtonItem];
Run Code Online (Sandbox Code Playgroud)

我还没有对此进行测试,但它应该符合您的需求。