NSArray *arr = @[@"消息",@"通知"];
UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems:arr];
segmentedControl.frame = CGRectMake(0, 0, 100, 30);
segmentedControl.selectedSegmentIndex = 0;
segmentedControl.tintColor = [UIColor blueColor];
segmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;
[segmentedControl addTarget:self action:@selector(indexDidChangeForSegmentedControl:)
forControlEvents:UIControlEventValueChanged];
// self.navigationController.navigationItem.titleView = segmentedControl;
[self.navigationItem setTitleView:segmentedControl];
}
- (void)indexDidChangeForSegmentedControl:(id)sender {
if ([sender selectedSegmentIndex] == 0) {
self.view.backgroundColor = [UIColor redColor];
} else if ([sender selectedSegmentIndex] == 1) {
self.view.backgroundColor = [UIColor blueColor];
}
}
网友评论