NSArray *titleArr = @[STR(@"全部"), STR(@"未使用"), STR(@"已赠送"), STR(@"已使用"), STR(@"已过期")]; [self.typeSegmentControl removeAllSegments]; CGFloat width = [UIScreen mainScreen].bounds.size.width/titleArr.count; for (NSString *titleStr in titleArr) { NSUInteger index = [titleArr indexOfObject:titleStr]; [self.typeSegmentControl insertSegmentWithTitle:titleStr atIndex:index animated:YES]; [self.typeSegmentControl setWidth:width forSegmentAtIndex:index]; } self.typeSegmentControl.selectedSegmentIndex = 0; self.typeSegmentControl.layer.masksToBounds = YES; NSDictionary *dicSelected = [NSDictionary dictionaryWithObjectsAndKeys:[UIColor redColor],NSForegroundColorAttributeName,[UIFont fontWithName:@"Helvetica" size:16.f],NSFontAttributeName,nil]; [self.typeSegmentControl setTitleTextAttributes:dicSelected forState:UIControlStateSelected]; NSDictionary *dicDisabled = [NSDictionary dictionaryWithObjectsAndKeys:[UIColor blackColor],NSForegroundColorAttributeName,[UIFont fontWithName:@"Helvetica" size:16.f],NSFontAttributeName,nil]; [self.typeSegmentControl setTitleTextAttributes:dicDisabled forState:UIControlStateNormal]; self.typeSegmentControl.tintColor = [UIColor whiteColor]; [self.typeSegmentControl setBackgroundColor:[UIColor whiteColor]]; self.typeIndex = self.typeSegmentControl.selectedSegmentIndex;
效果图如下: