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

时间:2013-10-04 11:01:27

标签: ios cocoa-touch ios7 uisegmentedcontrol uiappearance


如何更改iOS7中分段控制器的边框颜色而不更改文本颜色?


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

另请注意,文本(以及片段之间的线条)具有用[segmCtrl setTintColor:choosenTintColor]

设置的颜色

3 个答案:

答案 0 :(得分:54)

链接的答案确实回答了你的问题,但你必须在两行之间阅读。以下是更改应用程序中所有分段控件样式的更清晰示例:

// 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];

对于app中的一个控件:

// 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

答案 1 :(得分:11)

所以我自己解决了这个问题。我的解决方案为分段控件的边框提供了另一种颜色,没有别的。

为了仅更改分段控件的边框颜色,我将另一个分段控件放在旧的控件之上。我已禁用此新用户的用户互动,并将所选段的图片设置为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];

答案 2 :(得分:1)

我决定为所有项目添加viewWillAppear mySegmentControl.selectedIndex。 因此,所有段都会显示色调颜色。 当然,选择所有项目后,再次选择您的默认项目。