Mik*_*ike 69 iphone colors uisegmentedcontrol
有没有办法自定义所选分段的颜色UISegmentedControl?
我找到了segmentedController.tintColor属性,它可以让我自定义整个分段控件的颜色.问题是,当我为tintColor属性选择亮色时,所选的段几乎变得不可识别(其颜色几乎与分段控件的其余部分相同,因此很难区分选定和未选择的段).所以我不能使用任何好的亮色进行分段控制.解决方案将是选定的段颜色的一些单独的属性,但我找不到它.有没人解决这个问题?
whi*_*and 74
这是将选定的段更改为任何RGB颜色的绝对最简单的方法.不需要子类化或黑客攻击.
segmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;
UIColor *newTintColor = [UIColor colorWithRed: 251/255.0 green:175/255.0 blue:93/255.0 alpha:1.0];
segmentedControl.tintColor = newTintColor;
UIColor *newSelectedTintColor = [UIColor colorWithRed: 0/255.0 green:175/255.0 blue:0/255.0 alpha:1.0];
[[[segmentedControl subviews] objectAtIndex:0] setTintColor:newSelectedTintColor];
Run Code Online (Sandbox Code Playgroud)
此示例显示了重要的步骤:
笔记:
jot*_*chi 52
我在UISegmentcontrol中找到了为选定段添加颜色的简单方法
sender是UISegmentControl
for (int i=0; i<[sender.subviews count]; i++)
{
if ([[sender.subviews objectAtIndex:i]isSelected] )
{
UIColor *tintcolor=[UIColor colorWithRed:127.0/255.0 green:161.0/255.0 blue:183.0/255.0 alpha:1.0];
[[sender.subviews objectAtIndex:i] setTintColor:tintcolor];
}
else
{
[[sender.subviews objectAtIndex:i] setTintColor:nil];
}
}
Run Code Online (Sandbox Code Playgroud)
检查它为我工作
Dav*_*son 22
要做到这一点,您只需要找到所选的段,例如通过迭代分段控件的子视图并测试isSelected属性,然后只需setTintColor:在该子视图上调用该方法.
我通过将一个动作连接到Interface Builder中的ValueChanged事件的每个分段控件来实现这一点,我将它们连接到视图控制器文件中的这个方法,这实际上是msprague的答案:
- (IBAction)segmentedControlValueChanged:(UISegmentedControl*)sender
{
for (int i=0; i<[sender.subviews count]; i++)
{
if ([[sender.subviews objectAtIndex:i] respondsToSelector:@selector(isSelected)] && [[sender.subviews objectAtIndex:i]isSelected])
{
[[sender.subviews objectAtIndex:i] setTintColor:[UIColor whiteColor]];
}
if ([[sender.subviews objectAtIndex:i] respondsToSelector:@selector(isSelected)] && ![[sender.subviews objectAtIndex:i] isSelected])
{
[[sender.subviews objectAtIndex:i] setTintColor:[UIColor blackColor]];
}
}
}
Run Code Online (Sandbox Code Playgroud)
为确保每次用户打开视图时都能正确显示控件,我还必须重写-(void)viewDidAppear:animated方法并按如下方式调用方法:
-(void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
//Ensure the segmented controls are properly highlighted
[self segmentedControlValueChanged:segmentedControlOne];
[self segmentedControlValueChanged:segmentedControlTwo];
}
Run Code Online (Sandbox Code Playgroud)
对于某些奖励积分,如果您确实要将分段控件设置为在选择时使用白色调颜色,那么您还需要在选择文本时将其颜色更改为黑色,您可以这样做:
//Create a dictionary to hold the new text attributes
NSMutableDictionary * textAttributes = [[NSMutableDictionary alloc] init];
//Add an entry to set the text to black
[textAttributes setObject:[UIColor blackColor] forKey:UITextAttributeTextColor];
//Set the attributes on the desired control but only for the selected state
[segmentedControlOne setTitleTextAttributes:textAttributes forState:UIControlStateSelected];
Run Code Online (Sandbox Code Playgroud)
随着iOS 6的引入在viewDidAppear方法中第一次设置所选项目的色调颜色不起作用,为了解决这个问题,我使用了大中央调度来在几分之一秒之后更改所选颜色,如下所示:
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 0.05 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
[self segmentedControlValueChanged:segmentedControlOne];
});
Run Code Online (Sandbox Code Playgroud)
出于某种原因,Apple不允许您更改标准UISegmentedControls的颜色.
然而,它有一种"合法"的方法,即将分段控制样式更改为UISegmentedControlStyleBar.这使得它看起来略有不同,你可能不喜欢,但它确实允许颜色.
NSArray *itemArray = [NSArray arrayWithObjects: @"One", @"Two", @"Three", nil];
UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems:itemArray];
Run Code Online (Sandbox Code Playgroud)
//更改栏样式和广告以查看然后释放分段控制器
segmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;
segmentedControl.tintColor = [UIColor colorWithRed:.9 green:.1 blue:.1 alpha:1];
[self.view addSubview:segmentedControl];
[segmentedControl release];
Run Code Online (Sandbox Code Playgroud)
希望这有帮助,
Seb Kade"我在这里帮忙"
编辑:此解决方案在iOS 6上不起作用.请参阅下面的David Thompson的答案.
这个帖子真的很旧,但没有一个简单的答案对我有用.
只要您还原取消选择的分段控件的颜色,接受的答案就会起作用.这样的东西将在你的价值改变功能中起作用:
for (int i=0; i<[control.subviews count]; i++)
{
if ([[control.subviews objectAtIndex:i]isSelected] )
{
UIColor *tintcolor=[UIColor colorWithRed:127.0/255.0 green:161.0/255.0 blue:183.0/255.0 alpha:1.0];
[[control.subviews objectAtIndex:i] setTintColor:tintcolor];
} else {
UIColor *tintcolor=[UIColor grayColor]; // default color
[[control.subviews objectAtIndex:i] setTintColor:tintcolor];
}
}
Run Code Online (Sandbox Code Playgroud)
这是我修改过的uihacker的CustomSegmentedControl版本(请参阅评论中的信用).我的想法是改变找到应该改变tintColor的子视图的方法,从使用selectedIndex到isSelected方法.因为我正在使用自定义UISegmentedControl,它有3个或更多段,子视图排序随机变化(即使uihacker的"hasSetSelectedIndexOnce"标志也不能解决这个问题!).代码仍处于早期开发阶段,因此使用它需要您自担风险.欢迎任何评论:)
此外,我添加了对界面构建器的支持,并覆盖setSelectedSegmentIndex,以便它也更新颜色.请享用!
CustomSegmentedControl.h
//
// CustomSegmentedControl.h
//
// Created by Hlung on 11/22/54 BE.
// Copyright (c) 2554 __MyCompanyName__. All rights reserved.
//
// Credit: http://uihacker.blogspot.com/2010/05/iphone-uisegmentedcontrol-custom-colors.html
@interface CustomSegmentedControl : UISegmentedControl {
UIColor *offColor,*onColor;
}
@property (nonatomic,retain) UIColor *offColor,*onColor;
-(id)initWithItems:(NSArray *)items offColor:(UIColor*)offcolor onColor:(UIColor*)oncolor;
@end
Run Code Online (Sandbox Code Playgroud)
CustomSegmentedControl.m
#import "CustomSegmentedControl.h"
@interface CustomSegmentedControl (private)
-(void)setInitialMode;
-(void)toggleHighlightColors;
@end
@implementation CustomSegmentedControl
@synthesize offColor,onColor;
-(id)initWithItems:(NSArray *)items offColor:(UIColor*)offcolor onColor:(UIColor*)oncolor {
if (self = [super initWithItems:items]) {
// Initialization code
self.offColor = offcolor;
self.onColor = oncolor;
[self setInitialMode];
// default to 0, other values cause arbitrary highlighting bug
[self setSelectedSegmentIndex:0];
}
return self;
}
- (void)awakeFromNib {
// default colors
self.offColor = [UIColor colorWithWhite:0.8 alpha:1];
self.onColor = self.tintColor;
[self setInitialMode];
[self setSelectedSegmentIndex:0];
}
-(void)setInitialMode
{
// set essential properties
[self setBackgroundColor:[UIColor clearColor]];
[self setSegmentedControlStyle:UISegmentedControlStyleBar];
// loop through children and set initial tint
for( int i = 0; i < [self.subviews count]; i++ )
{
[[self.subviews objectAtIndex:i] setTintColor:nil];
[[self.subviews objectAtIndex:i] setTintColor:offColor];
}
// listen for updates, [self setSelectedSegmentIndex:0] triggers UIControlEventValueChanged in 5.0, 4.3 doesn't (facepalm), use if( self.window ) to fix this
[self addTarget:self action:@selector(toggleHighlightColors) forControlEvents:UIControlEventValueChanged];
}
// ---------------
// hlung's version
// ---------------
-(void)toggleHighlightColors
{
// the subviews array order randomly changes all the time, change to check for "isSelected" instead
for (id v in self.subviews) {
if ([v isSelected]) [v setTintColor:onColor];
else [v setTintColor:offColor];
}
}
// override: update color when set selection
- (void)setSelectedSegmentIndex:(NSInteger)selectedSegmentIndex {
[super setSelectedSegmentIndex:selectedSegmentIndex];
[self toggleHighlightColors];
}
// ---------------
@end
Run Code Online (Sandbox Code Playgroud)