繁体   English   中英

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

[英]UISegmentedControl in the Navigation Bar with the Back button

我正在以编程方式在导航栏中添加一个UISegmentedControl ,其中titleView应该是。 但正如Apple文档titleView下提到的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;

还有另一种方法可以添加UISegmentedControl和Back按钮吗?

谢谢。

试试这个

删除这一行---> 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;

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

[statFilter sizeToFit];

希望这可以帮助 !!!

您可以使用自定义视图创建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];

我没有对此进行测试,但它应该沿着您需要的正确线路进行测试。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM