Iphone 从UISegmentedControl中删除白色渐变
Iphone 从UISegmentedControl中删除白色渐变,iphone,objective-c,ios,cocoa-touch,uisegmentedcontrol,Iphone,Objective C,Ios,Cocoa Touch,Uisegmentedcontrol,如何在UISegmentedControl(条形样式)中消除或更改白色渐变?我找到的唯一方法是在每个线段上绘制一个图层。 坏消息是,每次选定的片段更改时,您必须自己对其进行着色(并对未选定的片段进行“脱色”) 好消息是,您可以为每个片段设置许多不同的颜色集,并在绿色附近显示红色,在蓝色附近显示绿色 当segmentedControl更改时,您可以调用您的方法: - (IBAction)changeSection:(id)sender { UISegmentedControl *segme
如何在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颜色=设置为第二种颜色(3=4)
initialStateArraySegmentedControl是segmentedControl的初始数组(单击时可能会出现顺序错误,但您需要初始数组),因此在您的init中尝试以下操作:
initialStateArraySegmentedControl = self.segmentedControl.subviews;
[self setColorForBackGround:self.segmentedControl withColor1:myUIColor1 withColor2:myUIColor2];
最后一行用于保持分段控件主视图周围的角
以及:
PS
您需要导入quartz框架@andy:源代码?你是说“密码”吗?是的,这几乎是您所需要的,您只需将UIColor和initialStateArraySegmentedControl声明为NSArray。。。将此代码放入拥有分段控件的UIViewController。。。你还需要什么?你有什么疑问?这似乎消除了分段控件上的圆角。有没有办法留住他们?
- (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] ];
///
}