从UISegmentedControl中删除白色渐变 [英] removing the white gradient from UISegmentedControl

查看:150
本文介绍了从UISegmentedControl中删除白色渐变的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

>

如何我摆脱了,或者改变UISegmentedControl(Bar style)中的白色渐变

How do i get rid of, or change the white gradient within UISegmentedControl (Bar style)??

推荐答案

在每个段上绘制一个图层。
坏消息是,那么你必须自己着色所选段每次改变(和decolonizee未选择)
好​​消息是,你可以有许多不同的颜色为每一个单个细分并在靠近蓝色的地方显示红色...

The only way i found is to draw a layer over every segment. the bad news is that then you have to colorize by yourself the selected segment every time it changes (and "decolonizee" the unselected) the good news is that you can have many different sets of colors for every single segment and display a red near a green near a blue...

当segmentedControl更改时,您可以调用该方法:

you can call your method when a segmentedControl change:

- (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)。

where myColor1 and 2 are the neutral color (UIColor) for unselected segments and 3 + 4 for selected in your case, if you want just one color make the 1 color = to the second (and 3=4).

initialStateArraySegmentedControl是segmentedControl的初始数组(当点击时它可能会顺序,但是你需要初始数组),因此在你的初始化尝试这样:

initialStateArraySegmentedControl is the initial array of your segmentedControl (it may chance order when clicked, but yu need the initial one), so in your init try this:

initialStateArraySegmentedControl = self.segmentedControl.subviews;
[self setColorForBackGround:self.segmentedControl withColor1:myUIColor1  withColor2:myUIColor2];

最后一行是保持分段控件主视图周围的角落

this last line is to keep the corner around the segmented control main view

和:

- (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
石英框架

PS you need to import the quartz framework

这篇关于从UISegmentedControl中删除白色渐变的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆