如何更改iOS7中UISegmentedControl边框的颜色?

如何在不改变文本颜色的情况下更改iOS7中分段控制器的边框颜色?

如果我可以保持段之间的界线(即与文本相同的颜色),那么这将是理想的,但是如果边界颜色改变意味着这条线的改变,那也是可以的。

还要注意,文本(以及段之间的线条)具有设置的颜色
[segmCtrl setTintColor:choosenTintColor]

链接的答案确实回答你的问题,但你必须读取行之间。 这里有一个更清晰的例子来更改应用程序中的所有分段控制样式:

 // Sets the tint color which typically sets the color of the segment images, text, dividers, // borders, and selected segment. A translucent version of this color is also used to tint a // segment when it is pressed and transitioning to being selected, as shown on the first // segment below. [[UISegmentedControl appearance] setTintColor:[UIColor blackColor]]; // The attributes dictionary can specify the font, text color, text shadow color, and text // shadow offset for the title in the text attributes dictionary [[UISegmentedControl appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor redColor]} forState:UIControlStateNormal]; 

对于应用程序中的一个控件:

 // Sets the tint color which typically sets the color of the segment images, text, dividers, // borders, and selected segment. A translucent version of this color is also used to tint a // segment when it is pressed and transitioning to being selected, as shown on the first // segment below. self.segControl.tintColor = [UIColor blackColor]; // The attributes dictionary can specify the font, text color, text shadow color, and text // shadow offset for the title in the text attributes dictionary [self.segControl setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor redColor]} forState:UIControlStateNormal]; 

更多信息在这里: https : //developer.apple.com/library/content/documentation/UserExperience/Conceptual/UIKitUICatalog/UISegmentedControl.html

所以我自己解决了这个问题。 我的解决scheme给了分段控制的边界另一种颜色,没有别的。

为了只改变分段控制的边界颜色,我把另一个分段控制放在我的旧控制的顶部。 我禁用了这个新的用户交互,并将选定的段的图像设置nil

 UIView *header = [[UIView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 40)]; // Header view for my main view UISegmentedControl *subCat = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:@"Segm 1", @"Segm 2", @"Segm 3", @"Segm 4", nil]]; // The UISegmentedController which I want to change color for [subCat setFrame:CGRectMake(5, 5, [UIScreen mainScreen].bounds.size.width - 10, 30)]; [subCat setSelectedSegmentIndex:0]; UISegmentedControl *bcg = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:@" ", @" ", @" ", @" ", nil]]; // The UISegmentedController I put on top of the other one UIColor *subColor = [UIColor redColor]; [subCat setTintColor:subColor]; [bcg setFrame:CGRectMake(5, 5, [UIScreen mainScreen].bounds.size.width - 10, 30)]; [bcg setTintColor:[UIColor greenColor]]; [bcg setUserInteractionEnabled:NO]; [bcg setSelectedSegmentIndex:0]; [bcg setImage:nil forSegmentAtIndex:0]; // Removing highlight color [header addSubview:subCat]; [header addSubview:bcg]; [[self view] addSubview:header]; 

我解决了所有项目的viewWillAppear mySegmentControl.selectedIndex。 所以,所有部分都会出现色调颜色。 当然,选定所有项目后,再次select您的默认项目。