【发布时间】:2016-01-12 23:39:58
【问题描述】:
I want the UISegmentedcontrol like this
我想更改选中和未选中段的颜色。 谁能帮帮我?
【问题讨论】:
-
这很可能是
NSView的自定义子类 -
我希望选中的段颜色为浅蓝色,未选中的段为深蓝色
标签: ios objective-c uisegmentedcontrol
I want the UISegmentedcontrol like this
我想更改选中和未选中段的颜色。 谁能帮帮我?
【问题讨论】:
NSView的自定义子类
标签: ios objective-c uisegmentedcontrol
试试这样的
self.segmantControl.backgroundColor = [UIColor colorWithRed:114.0/255. green:197./255. blue:182./255. alpha:1.0];
// 如果你在 IB/storyboard 中没有约束,你应该以编程方式设置高度约束
NSLayoutConstraint *constraint = [NSLayoutConstraint constraintWithItem:self.segmantControl
attribute:NSLayoutAttributeHeight
relatedBy:NSLayoutRelationEqual
toItem:nil
attribute:NSLayoutAttributeWidth
multiplier:1
constant:55];
[self.segmantControl addConstraint:constraint];
// 如果你在 IB/storyboard 中有约束,你应该将属性设置为高度约束,名称为 (segmentHeight)
self.segmentHeight.constant = 55; // height constraint, U should add constraints to your segment control and set property to height constraint, and change it
self.segmantControl.layer.cornerRadius = 25.0;
self.segmantControl.layer.borderColor = [UIColor whiteColor].CGColor;
self.segmantControl.layer.borderWidth = 1.0f;
self.segmantControl.layer.masksToBounds = YES;
// color selected text ---> whiteColor
[[UISegmentedControl appearance] setTitleTextAttributes:@{ NSForegroundColorAttributeName : [UIColor whiteColor] } forState:UIControlStateSelected];
// color disabled text ---> whiteColor
[[UISegmentedControl appearance] setTitleTextAttributes:@{ NSForegroundColorAttributeName : [UIColor whiteColor] } forState:UIControlStateNormal];
UIColor *newSelectedTintColor = [UIColor colorWithRed:109./255. green:175./255. blue:179./255 alpha:1.0];
[[[self.segmantControl subviews] objectAtIndex:0] setTintColor:newSelectedTintColor];//selected left segment color
[[[self.segmantControl subviews] objectAtIndex:1] setTintColor:newSelectedTintColor];// selected right segment colour
【讨论】:
试试这个
self.segmentedControl.tintColor = [UIColour greenColor];
单击任何段后,更改所选段的颜色:
- (void)selectedSegment:(UISegmentedControl *)sender
{
for (int i=0; i<[sender.subviews count]; i++)
{
if ([[sender.subviews objectAtIndex:i]isSelected])
{
UIColor *tintcolor = [UIColor blueColor];
[[sender.subviews objectAtIndex:i] setTintColor:tintcolor];
}
else
{
[[sender.subviews objectAtIndex:i] setTintColor:nil];
}
}
}
【讨论】: