如何在UISegmentedControl(栏样式)中摆脱或更改白色渐变?
最佳答案
我发现的唯一方法是在每个线段上绘制一层。
坏消息是,您每次更改时都必须自己为选定的段着色(并“取消殖民”未选中的段)
好消息是您可以为每个段设置许多不同的颜色,并在绿色附近显示红色,在蓝色附近显示...
您可以在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 color =设置为第二种颜色(和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] ];
///
}
聚苯乙烯
您需要导入石英框架
关于iphone - 从UISegmentedControl移除白色渐变,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/8551510/