我在我的应用程序中使用 UISegmentedControl ,我可以在更改状态时切换文本颜色 . 但默认情况下,所选片段包含不同的色调,如下图所示
UISegmentedControl
当它处于选定状态时,我不希望有不同的色调颜色 . 我只想通过文字颜色区分选定的片段,如下图所示 .
我知道这是一个愚蠢的问题,但其他类似的问题都没有给我一个正确的答案 .
你能指导我实现这个目标吗?
在期待中感谢!
我强烈建议不要这样做 .
你意识到这只会让它更难使用吗?对于一个色盲人(大约十一个男人是colourblind),你的版本几乎不可能使用 . 对于视力不佳的人来说,使用起来非常困难 . 可用性的最佳测试(以及Apple工程师的建议)是将其转换为灰度图像,看看是否仍然可以使用它 .
例如...
Section tint
成为...
在所选索引上使用对比色文本可以改善这一点 .
Your colour scheme
这很难确定选择哪个部分 .
这就够了 .
NSDictionary *attributes = [NSDictionary dictionaryWithObjectsAndKeys: [UIFont boldSystemFontOfSize:17], UITextAttributeFont, [UIColor blackColor], UITextAttributeTextColor, nil]; [_segment setTitleTextAttributes:attributes forState:UIControlStateNormal]; NSDictionary *highlightedAttributes = [NSDictionary dictionaryWithObject:[UIColor whiteColor] forKey:UITextAttributeTextColor]; [_segment setTitleTextAttributes:highlightedAttributes forState:UIControlStateHighlighted];
如果要在段更改时更改颜色
- (IBAction)horseSegmentChanged:(UISegmentedControl*)sender { if (sender.selectedSegmentIndex == 0) { NSDictionary *attributes = [NSDictionary dictionaryWithObjectsAndKeys: [UIFont boldSystemFontOfSize:17], UITextAttributeFont, [UIColor blackColor], UITextAttributeTextColor, nil]; [_segment setTitleTextAttributes:attributes forState:UIControlStateNormal]; NSDictionary *highlightedAttributes = [NSDictionary dictionaryWithObject:[UIColor whiteColor] forKey:UITextAttributeTextColor]; [_segment setTitleTextAttributes:highlightedAttributes forState:UIControlStateHighlighted]; } else if (sender.selectedSegmentIndex == 1) { // selected shared blogs } }
找到下面的customsegment控件
#import <UIKit/UIKit.h> #import <QuartzCore/QuartzCore.h> @interface CustomSegmentControl : UISegmentedControl @end #import "CustomSegmentControl.h" @interface CustomSegmentControl () - (UIImage *)imageWithColor:(UIColor *)color; @end @implementation CustomSegmentControl - (id)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { // Initialization code } return self; } - (void)drawRect:(CGRect)rect { [super drawRect:rect]; [self setBackgroundImage:[[UIImage imageNamed:@"img.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 0, 0, 0)] forState:UIControlStateNormal barMetrics:UIBarMetricsDefault]; [self setBackgroundImage:[[UIImage imageNamed:@"img.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 0, 0, 0)] forState:UIControlStateSelected barMetrics:UIBarMetricsDefault]; } @end
这将解决您的问题
我同意Fogmeister的观点,就可用性而言,这可能不是一个好主意 . 也就是说,UISegmentedControl允许您为特定状态设置背景图像 . 您可以为每个UISegmentedControl状态设置相同颜色的图像,这样可以获得您想要的效果 .
要以编程方式从UIColor创建图像,请参阅here .
以及一些带有始终为白色背景的分段控件的示例代码(考虑UIImage类别为上面引用的类别):
[self.segmentedControl setBackgroundImage:[UIImage imageWithColor:[UIColor whiteColor]] forState:UIControlStateNormal barMetrics:UIBarMetricsDefault]; [self.segmentedControl setBackgroundImage:[UIImage imageWithColor:[UIColor whiteColor]] forState:UIControlStateSelected barMetrics:UIBarMetricsDefault]; [self.segmentedControl setBackgroundImage:[UIImage imageWithColor:[UIColor whiteColor]] forState:UIControlStateHighlighted barMetrics:UIBarMetricsDefault];
4 回答
我强烈建议不要这样做 .
你意识到这只会让它更难使用吗?对于一个色盲人(大约十一个男人是colourblind),你的版本几乎不可能使用 . 对于视力不佳的人来说,使用起来非常困难 . 可用性的最佳测试(以及Apple工程师的建议)是将其转换为灰度图像,看看是否仍然可以使用它 .
例如...
Section tint
成为...
在所选索引上使用对比色文本可以改善这一点 .
Your colour scheme
成为...
这很难确定选择哪个部分 .
这就够了 .
如果要在段更改时更改颜色
找到下面的customsegment控件
这将解决您的问题
我同意Fogmeister的观点,就可用性而言,这可能不是一个好主意 . 也就是说,UISegmentedControl允许您为特定状态设置背景图像 . 您可以为每个UISegmentedControl状态设置相同颜色的图像,这样可以获得您想要的效果 .
要以编程方式从UIColor创建图像,请参阅here .
以及一些带有始终为白色背景的分段控件的示例代码(考虑UIImage类别为上面引用的类别):