ios - 带有后退按钮的导航栏中的 UISegmentedControl

标签 ios objective-c uinavigationcontroller uinavigationbar uisegmentedcontrol

我正在以编程方式将 UISegmentedControl 添加到导航栏,该位置应该是 titleView。但是作为Apple docstitleView 下已经提到,如果 leftBarButtonItem 不为 nil,则忽略此属性

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

enter image description here

下面是我添加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 以及后退按钮吗?

谢谢。

最佳答案

试试这个

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

希望这对您有帮助!!!

关于ios - 带有后退按钮的导航栏中的 UISegmentedControl,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15383014/

相关文章:

ios - 添加 NSOperation 崩溃

ios - 如何在 iOS 上使用 CoreMIDI?

ios - xcode UITests 检查测试是否在真实设备上运行

ios - 为 iOS 应用程序换肤

c++ - 链接 Objective-C++

objective-c - 如何从我的类列表中找到从特定类继承的类?

ios - 从 itms-services 下载时如何打开下载的应用程序?

iphone - dismissModalViewControllerAnimated 不工作

objective-c - 从 AppDelegate 更改选项卡和推送 View

ios - 应用程序设计 : UITabBarController inside UINavigationController