ios 如何在 iOS7 中更改 UISegmentedControl 边框的颜色?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19179599/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to change color of UISegmentedControl border in iOS7?
提问by OscarWyck
How do I change the border color of the segmented controller in iOS7 without changing the text color?
如何在不改变文字颜色的情况下,在 iOS7 中更改分段控制器的边框颜色?
It would be ideal if I could keep the line between the segments as is (i.e. same color as the text), but if a border color change implies a change of this line it would also be ok.
如果我可以保持线段之间的线保持原样(即与文本颜色相同),那将是理想的,但是如果边框颜色的变化意味着这条线的变化也可以。
Note also that the text (and the lines between segments) have the color which is set with [segmCtrl setTintColor:choosenTintColor]
另请注意,文本(以及段之间的线条)的颜色设置为 [segmCtrl setTintColor:choosenTintColor]
采纳答案by OscarWyck
So I solved the problem myself. My solution gives the border of the segmented control another color, nothing else.
所以我自己解决了这个问题。我的解决方案为分段控件的边框提供了另一种颜色,仅此而已。
In order to only change the border color of the segmented control, I put another segmented control on top of my old one. I disabled user interaction for this new one, and I set the image for the selected segment to nil
.
为了仅更改分段控件的边框颜色,我在旧控件的顶部放置了另一个分段控件。我禁用了这个新的用户交互,并将所选片段的图像设置为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];
回答by iwasrobbed
The linked answer does indeed answer your question, but you have to read between the lines. Here's a clearer example to change all segmented control styles within the 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.
[[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];
For one control within the 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];
More info here: https://developer.apple.com/library/content/documentation/UserExperience/Conceptual/UIKitUICatalog/UISegmentedControl.html
更多信息在这里:https: //developer.apple.com/library/content/documentation/UserExperience/Conceptual/UIKitUICatalog/UISegmentedControl.html
回答by Claudio Castro
I resolve putting in viewWillAppear mySegmentControl.selectedIndex for all items. So, the tint color appear for all segments. Of course, after selected All items, select your default item again.
我决定为所有项目放入 viewWillAppear mySegmentControl.selectedIndex 。因此,所有段都会出现色调颜色。当然,选择所有项目后,再次选择您的默认项目。