效果图:
代码:
NSArray *segmentTextContent = [NSArray arrayWithObjects: @"Check", @"Search", @"Tools", nil]; UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems:segmentTextContent]; CGRect frame = CGRectMake(50, 100,self.view.bounds.size.width - (50 * 2.0),40); segmentedControl.frame = frame; [segmentedControl addTarget:self action:@selector(segmentAction:) forControlEvents:UIControlEventValueChanged]; segmentedControl.segmentedControlStyle = UISegmentedControlStyleBordered; segmentedControl.selectedSegmentIndex = 1; [self.view addSubview:segmentedControl]; [segmentedControl release];
2.UISegmentedControl 放在 UIToolBar上面
效果图:
代码:
NSArray *array = @[@"列表",@"日",@"月"]; UISegmentedControl *modePicker = [[UISegmentedControl alloc]initWithItems:array]; modePicker.segmentedControlStyle = UISegmentedControlStyleBar; [modePicker setWidth:50 forSegmentAtIndex:0]; [modePicker setWidth:50 forSegmentAtIndex:1]; [modePicker setWidth:50 forSegmentAtIndex:2]; NSString *todayTitle = @"今天"; UIBarButtonItem *todayButton = [[UIBarButtonItem alloc] initWithTitle:todayTitle style:UIBarButtonItemStyleBordered target:self action:@selector(todayButtonTapped:)]; UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithCustomView:modePicker];
[modePicker release]
UIBarButtonItem *fixedSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil]; [fixedSpace setWidth:20];
[self setToolbarItems:@[todayButton, fixedSpace, item] animated:NO];
[[self navigationController] setToolbarHidden:NO animated:NO];
[todayButton release];
[item release];
[fixedSpace release];