2011-02-19 104 views
0

我想把这个在导航栏中,但没有显示出来,你可以在 看看吗?分段控制导航栏

UISegmentedControl *seg1 = [[UISegmentedControl alloc] 
initWithItems:[NSArray arrayWithObjects:@"von mir", @"alle", nil]]; 
[seg1 setSegmentedControlStyle:UISegmentedControlStyleBar]; 
UIBarButtonItem *barItem = [[UIBarButtonItem alloc] initWithCustomView:seg1]; 
UIBarButtonItem *flexItem = [[UIBarButtonItem alloc] 
initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace 
target:self action:nil]; 
[self.navigationController.navigationBar setItems:[NSArray 
arrayWithObjects:flexItem, barItem, flexItem, nil]]; 
[flexItem release]; 
[barItem release]; 
[seg1 release]; 
+0

只是一个想法,不能使用IB来做到这一点。 – Robin 2011-02-19 04:46:47

+0

是的,但我因为某种原因与代码接口 – Maysam 2011-02-19 05:30:58

回答

6

UINavigationBaritems属性只接受UINavigationItem目的,不UIBarButtonItem对象的数组。您不能像使用UIToolbar一样配置导航栏。取而代之的是,在您的视图控制器,这样做:

UISegmentedControl * seg1 = [[UISegmentedControl alloc] 
    initWithItems:[NSArray arrayWithObjects:@"von mir", @"alle", nil]]; 
[seg1 setSegmentedControlStyle:UISegmentedControlStyleBar]; 
self.navigationItem.titleView = seg1; 

这增加了分段控制到您的视图控制器的导航项目,这是出现在导航栏上为中心的自定义视图的标题视图。