在选项卡的中心设置分段控件

时间:2011-08-19 05:39:47

标签: iphone objective-c cocoa-touch uisegmentedcontrol

我正在使用分段控件。我使用以下代码将其设置在选项卡的左侧。

 segmentedControl = [[UISegmentedControl alloc] initWithItems:nil];
 segmentedControl.segmentedControlStyle =
 UISegmentedControlStyleBezeled;
 segmentedControl.frame = CGRectMake(100,10,220,35);
 [segmentedControl insertSegmentWithTitle:@"Male" atIndex:0
 animated:YES];
 [segmentedControl insertSegmentWithTitle:@"Female" atIndex:1
 animated:YES];
 segmentedControl.selectedSegmentIndex = 1;
 [segmentedControl setMomentary:NO];
 [segmentedControl addTarget:self action:@selector(segmentSwitch:)
 forControlEvents:UIControlEventValueChanged];

 UIBarButtonItem *segmentBarItem = [[UIBarButtonItem alloc]
 initWithCustomView:segmentedControl];
 [segmentedControl release];
 self.navigationItem.leftBarButtonItem = segmentBarItem;
 [segmentBarItem release];

我的问题是如何将它设置在标签栏的中心,因为它没有采用CGRect()值来设置位置。

1 个答案:

答案 0 :(得分:2)

我认为您正在尝试将 UISegmentedControl 添加到 UINavigationBar 而不是 UITabBar 。如果您希望它位于中心,您可以将其设置为 navigationItem titleView

self.navigationItem.titleView = segmentedControl;