导航栏中的UISegmentedControl和后退button

我正在编程添加一个UISegmentedControl到导航栏的地方应该是titleView 。 但是,正如苹果文档在titleViewtitleView如果leftBarButtonItem不为零 ,则该属性将被忽略

但我想要有后退button。 就像他们用自己的形象说明的一样!

在这里输入图像说明

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

谢谢。

尝试这个

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

希望这可以帮助 !!!

你可以创build一个自定义视图的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]; 

我没有testing过,但应该沿着你所需要的正确的路线。