/** 设置选择器 */ - (void)setUpSegmentCtr { UISegmentedControl *segmentCtr = [[UISegmentedControl alloc] initWithItems:@[@"采购订单",@"销售订单"]]; self.navigationItem.titleView = segmentCtr; [segmentCtr addTarget:self action:@selector(didClicksegmentedControlAction:) forControlEvents:UIControlEventValueChanged]; self.segmentCtr = segmentCtr; NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:EMCNavColor, NSForegroundColorAttributeName, [UIFont boldSystemFontOfSize:14], NSFontAttributeName,nil]; [ self.segmentCtr setTitleTextAttributes:dic forState:UIControlStateSelected]; NSDictionary* unselectedTextAttributes = @{NSFontAttributeName:[UIFont boldSystemFontOfSize:14], NSForegroundColorAttributeName: [UIColor whiteColor]}; [self.segmentCtr setTitleTextAttributes:unselectedTextAttributes forState:UIControlStateNormal]; [self.segmentCtr setBackgroundImage:[UIImage imageWithColor:[UIColor clearColor]] forState:UIControlStateNormal barMetrics:UIBarMetricsDefaultPrompt]; [self.segmentCtr setBackgroundImage:[UIImage imageWithColor:[UIColor whiteColor]] forState:UIControlStateSelected barMetrics:UIBarMetricsDefaultPrompt]; self.segmentCtr.tintColor = [UIColor whiteColor]; self.segmentCtr.apportionsSegmentWidthsByContent = NO; self.segmentCtr.backgroundColor = [UIColor clearColor]; self.segmentCtr.selectedSegmentIndex = 0; }
再viewdidload中添加
- (void)viewDidLoad { [super viewDidLoad]; /** 设置选择器 */ [self setUpSegmentCtr]; }
效果图如下: