如何在UISegmentedControl(Bar style)中摆脱或更改白色渐变?
答案 0 :(得分:1)
我找到的唯一方法是在每个细分上绘制一个图层。 坏消息是,每当它改变时你必须自己着色选定的段(并且“取消选择”未选择的段) 好消息是,你可以为每一个细分市场提供多种不同的颜色,并在蓝色附近的绿色附近显示红色...
您可以在segmentedControl更改时调用您的方法:
- (IBAction)changeSection:(id)sender {
UISegmentedControl *segmetedControl = sender;
[self colorize2SegmentsWithSelected:segmetedControl.selectedSegmentIndex];
// (...)
}
并在您的方法中:
-(void)colorize2SegmentsWithSelected:(int)selected{
switch (selected) {
case 0:
[self myShineGradient:[initialStateArraySegmentedControl objectAtIndex:1] withColor1:myUIColor1 withColor2:myUIColor2];
[self myShineGradient:[initialStateArraySegmentedControl objectAtIndex:0] withColor1:myUIColor3 withColor2:myUIColor4];
break;
case 1:
[self myShineGradient:[initialStateArraySegmentedControl objectAtIndex:0] withColor1:myUIColor1 withColor2:myUIColor2];
[self myShineGradient:[initialStateArraySegmentedControl objectAtIndex:1] withColor1:myUIColor3 withColor2:myUIColor4];
break;
// (...)
}
// (...)
}
其中myColor1和2是未选定段的中性色(UIColor),选择3 + 4 在你的情况下,如果你只想要一种颜色,那么1种颜色=第二种颜色(和3 = 4)。
initialStateArraySegmentedControl是你的segmentedControl的初始数组(单击时可能会命令,但是你需要初始数组),所以在你的init中试试这个:
initialStateArraySegmentedControl = self.segmentedControl.subviews;
[self setColorForBackGround:self.segmentedControl withColor1:myUIColor1 withColor2:myUIColor2];
这最后一行是保持分段控件主视图周围的角落
和
- (void)myShineGradient:(UIView*)myView withColor1:(UIColor*)color1 withColor2:(UIColor*)color2
{
// remove old personal shine layer (if any exists):
int layerNumberNow = [[myView.layer sublayers] count];
if (layerNumberNow>2) {
[[[myView.layer sublayers] objectAtIndex:0] removeFromSuperlayer];
}
// add shine layer
CAGradientLayer *gradientLayer = [CAGradientLayer layer];
[gradientLayer setBounds:[myView bounds]];
// Center the layer inside the parent layer
[gradientLayer setPosition:
CGPointMake([myView bounds].size.width/2,
[myView bounds].size.height/2)];
// Set the colors for the gradient to the
// two colors specified for high and low
[gradientLayer setColors:
[NSArray arrayWithObjects:
(id)[color1 CGColor],(id)[color2 CGColor], nil]];
[myView.layer insertSublayer:gradientLayer atIndex:layerNumberNow-2];
}
- (void)setColorForBackGround:(UIView*)myView withColor1:(UIColor*)color1 withColor2:(UIColor*)color2
{
// add shine layer
CAGradientLayer *gradientLayer = [CAGradientLayer layer];
[gradientLayer setBounds:[myView bounds]];
// Center the layer inside the parent layer
[gradientLayer setPosition:
CGPointMake([myView bounds].size.width/2,
[myView bounds].size.height/2)];
// Set the colors for the gradient to the
// two colors specified for high and low
[gradientLayer setColors:
[NSArray arrayWithObjects:
(id)[color1 CGColor],(id)[color2 CGColor], nil]];
[myView.layer setBackgroundColor:[ [UIColor colorWithRed:0 green:0 blue:0 alpha:0] CGColor]];
[myView.layer setCornerRadius:4];
[[myView layer] setMasksToBounds:YES];
// Display a border around the button
// with a 1.0 pixel width
[[myView layer] setBorderWidth:1.0f];
[[myView layer] setBorderColor:[ [UIColor colorWithRed:1 green:1 blue:1 alpha:.1] CGColor] ];
///
}
PS 你需要导入石英框架