SegmentControl的多选项实现(标题栏)

时间:2023-03-08 19:20:32
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 = ;
self.typeSegmentControl.layer.masksToBounds = YES; NSDictionary *dicSelected = [NSDictionary dictionaryWithObjectsAndKeys:[UIColor redColor],NSForegroundColorAttributeName,[UIFont fontWithName:@"Helvetica" size:.f],NSFontAttributeName,nil];
[self.typeSegmentControl setTitleTextAttributes:dicSelected forState:UIControlStateSelected]; NSDictionary *dicDisabled = [NSDictionary dictionaryWithObjectsAndKeys:[UIColor blackColor],NSForegroundColorAttributeName,[UIFont fontWithName:@"Helvetica" size:.f],NSFontAttributeName,nil];
[self.typeSegmentControl setTitleTextAttributes:dicDisabled forState:UIControlStateNormal]; self.typeSegmentControl.tintColor = [UIColor whiteColor];
[self.typeSegmentControl setBackgroundColor:[UIColor whiteColor]];
self.typeIndex = self.typeSegmentControl.selectedSegmentIndex;

效果图如下:

SegmentControl的多选项实现(标题栏)