在viewDidload中添加如下代码
方法一:
toolBar = [[UIToolbar alloc]initWithFrame:CGRectMake(0.0, 325.0, 320.0, 44.0)]; toolBar.barStyle = UIBarStyleDefault; NSArray *segmentItem = [[NSArray alloc]initWithObjects:@"Day",@"List",@"Month", nil]; UISegmentedControl *segmentControll = [[UISegmentedControl alloc]initWithItems:segmentItem]; segmentControll.frame = CGRectMake(80.0, 325.0, 200.0, 30.0); //[self.toolBar addSubview:segmentControll]; [self.navigationController.toolbar addSubview:segmentControll]; [self.view addSubview:toolBar];
方法二:
UIBarButtonItem *segBarBtn; segBarBtn = [[UIBarButtonItem alloc] initWithCustomView:segmentControl];
Add the bar button to the tool bar,
NSArray *toolbarItems = [NSArray arrayWithObject:segBarBtn]; [toolbar setItems:toolbarItems animated:NO];