有没有办法自定义UISegmentedControl
中选定段的颜色?
我发现了segmentedController.tintColor
属性,它允许我自定义整个分段控件的颜色。
问题在于,当我为tintColor
属性选择明亮的颜色时,选定的段几乎无法被识别(它的颜色与其余分段控件的颜色几乎相同,因此很难区分选定和未选定的段)。
所以我不能使用任何好看的明亮颜色作为分段控制的颜色。
解决方案是一些单独的选定段颜色的属性,但我找不到这个属性。有人解决了这个问题吗?
有没有办法自定义UISegmentedControl
中选定段的颜色?
我发现了segmentedController.tintColor
属性,它允许我自定义整个分段控件的颜色。
问题在于,当我为tintColor
属性选择明亮的颜色时,选定的段几乎无法被识别(它的颜色与其余分段控件的颜色几乎相同,因此很难区分选定和未选定的段)。
所以我不能使用任何好看的明亮颜色作为分段控制的颜色。
解决方案是一些单独的选定段颜色的属性,但我找不到这个属性。有人解决了这个问题吗?
segmentControl.backgroundColor = customColor;
不确定这个是否会被应用商店批准,但我编写了一个UISegmentedControl的子类,可以让您设置自定义的选中和未选中颜色。查看注释以获取更多信息:
http://uihacker.blogspot.com/2010/05/iphone-uisegmentedcontrol-custom-colors.html
我想知道为什么没有人提到UIAppearanceProxy
苹果文档::
https://developer.apple.com/documentation/uikit/uisegmentedcontrol#1653545
示例代码:
private class func applyUISegmentControlAppearance(){
let apperance = UISegmentedControl.appearance()
// Set Navigation bar Title colour
let unselAttrib = [NSForegroundColorAttributeName:UIColor.yellow,
NSFontAttributeName: UIFont.systemFont(ofSize: 15)]
let selAttrib = [NSForegroundColorAttributeName:UIColor.red,
NSFontAttributeName: UIFont.boldSystemFont(ofSize: 15)]
apperance.setTitleTextAttributes(unselAttrib, for: .normal)
apperance.setTitleTextAttributes(selAttrib, for: .selected)
}
来自:
您可以在AppDelegate
中调用此方法
application(_ application: UIApplication, willFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey : Any]? = nil) -> Bool
我发现可以使用与段相同索引的子视图上的标记,以便无论以任何顺序它们都能正确着色。
// In viewWillAppear set up the segmented control
// then for 3 segments:
self.navigationItem.titleView = segmentedControl;
//Order of subviews can change randomly!, so Tag them with same index as segment
[[[segmentedControl subviews]objectAtIndex:0]setTag:0];
[[[segmentedControl subviews]objectAtIndex:1]setTag:1];
[[[segmentedControl subviews]objectAtIndex:2]setTag:2];
// color follows the selected segment
- (IBAction)mySelector:(id)sender {
selector = [sender selectedSegmentIndex]
for (id seg in [segmentedControl subviews]) {
for (id label in [seg subviews]) {
if ([seg tag] == selector){
[seg setTintColor:selectedColor];
} else {
[seg setTintColor:nonSelectedColor];
}
}
}
}
// in viewDidAppear for returning to the view
[segmentedControl setSelectedSegmentIndex:selector];
for (id seg in [segmentedControl subviews]) {
for (id label in [seg subviews]) {
if ([seg tag] == selector){
[seg setTintColor:selectedColor];
} else {
[seg setTintColor:nonSelectedColor];
}
}
}
当我在不同的段之间切换时,前两个解决方案对我没有用。
我的解决方案是在我的视图控制器中处理段变更事件,然后每次更改段时调用此方法:
+ (void)setSegmentedControl:(UISegmentedControl *)segmentedControl
selectedColor:(UIColor *)selectedColor
deselectedColor:(UIColor *)deselectedColor
{
for (int i = 0; i < segmentedControl.subviews.count; i++)
{
id subView = [segmentedControl.subviews objectAtIndex:i];
if ([subView isSelected])
[subView setTintColor:selectedColor];
else
[subView setTintColor:deselectedColor];
}
}
Try this solution.
@IBAction func dashBoardSegmentValueChanged(sender: AnyObject) {
switch dashBoardSegment.selectedSegmentIndex
{
case 0:
sender.subviews.last?.backgroundColor = UIColor.whiteColor()
sender.subviews.first?.backgroundColor = UIColor.clearColor()
break;
case 1:
sender.subviews.first?.backgroundColor = UIColor.whiteColor()
sender.subviews.last?.backgroundColor = UIColor.clearColor()
break;
default:
break;
}
}
Note: Make sure you select one segment subview as initial selected for easiness. It works if you have two segment subviews.
- (IBAction)segmentedControlValueChanged:(UISegmentedControl *)sender {
for (int i = 0; i < sender.subviews.count; i++) {
UIControl *component = [sender.subviews objectAtIndex:i];
if ([component respondsToSelector:@selector(isSelected)]) {
UIColor *selectedColor = [UIColor greenColor];
UIColor *normalColor = [UIColor blackColor];
UIColor *tint = component.isSelected ? selectedColor : normalColor;
[component setTintColor:tint];
}
}
}
我发现以上的答案非常有帮助。我正在使用分段控件来设置旋钮的精度。我综合了以上的答案,得出了以下代码:
-(void) viewDidLoad {
NSArray *segments = [NSArray arrayWithObjects:@"Course", @"Fine",nil];
[knob setPrecision:0.1]; // initial precision
// Set starting values
UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems:segments];
segmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;
segmentedControl.frame = CGRectMake(120, 680, 228, 30);
[segmentedControl addTarget:self action:@selector(precisionSelect:) forControlEvents:UIControlEventValueChanged];
segmentedControl.momentary = YES;
[self.view addSubview:segmentedControl];
}
- (void)precisionSelect:(UISegmentedControl*)sender
{
UIColor *tintcolor = [UIColor darkGrayColor];
if (sender.selectedSegmentIndex == 0) {
[[sender.subviews objectAtIndex:0] setTintColor:nil];
[[sender.subviews objectAtIndex:1] setTintColor:tintcolor];
[knob setPrecision:0.1]; // Coarse
} else {
[[sender.subviews objectAtIndex:0] setTintColor:tintcolor];
[[sender.subviews objectAtIndex:1] setTintColor:nil];
[knob setPrecision:0.05]; // Fine
}
}
希望这能帮助到其他人..
对我来说,关键是能够使用以下代码重置未选择的索引:setTintColor:nil];
#define kTagOffState 0
#define kTagOnState 2
#define UIColorFromRGB(rgbValue) [UIColor \
colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 \
green:((float)((rgbValue & 0xFF00) >> 8))/255.0 \
blue:((float)(rgbValue & 0xFF))/255.0 alpha:1.0]
//usage UIColor color = UIColorFromRGB(0xF7F7F7);
UIColor onColor = UIColorFromRGB(0xF7F7F7);
UIColor offColor = UIColorFromRGB(0x878787);
[multiStateControl setTag:kTagOffState forSegmentAtIndex:0];
[multiStateControl setTag:kTagOnState forSegmentAtIndex:1];
[multiStateControl setTintColor:onColor forTag:kTagOnState];
[multiStateControl setTintColor:offColor forTag:kTagOffState];
要做您这样的事情,可能需要访问未记录的功能和黑客技巧,这肯定会让苹果很生气,这可能会导致您的应用被拒绝。
现在,解决方案在于使用另一种技巧,您可以使用两个按钮,并在单击它们时交换它们的图像。将按钮放得更靠近,并使用半分段控件的图像来产生分段控件的幻觉,这是我能为您提供的全部建议。
希望这有所帮助。
谢谢,
Madhup