I am trying to put this in a navigationbar, but doesnt show up, can u have a look at it?
UISegmentedControl *seg1 = [[UISegmentedControl alloc]
initWithItems:[NSArray arrayWithObjects:@"von mir", @"alle", nil]];
[seg1 setSegmentedControlStyle:UISegmentedControlStyleBar];
UIBarButtonItem *barItem = [[UIBarButtonItem alloc] initWithCustomView:se开发者_JS百科g1];
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];
UINavigationBar
's items
property only accepts an array of UINavigationItem
objects, not UIBarButtonItem
objects. You can't configure a navigation bar the same way as you do a UIToolbar
. Instead, in your view controller, do this:
UISegmentedControl * seg1 = [[UISegmentedControl alloc]
initWithItems:[NSArray arrayWithObjects:@"von mir", @"alle", nil]];
[seg1 setSegmentedControlStyle:UISegmentedControlStyleBar];
self.navigationItem.titleView = seg1;
This adds the segmented control to the title view of your view controller's navigation item, which is a custom view that appears centered on the navigation bar.
精彩评论