开发者

segmentedcontrol in navigationbar

开发者 https://www.devze.com 2023-02-11 20:21 出处:网络
I am trying to put this in a navigationbar, but doesnt show up, can u have a look at it? UISegmentedControl *seg1 = [[UISegmentedControl alloc]

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消