具有自定义颜色的UISegmentedControl:分隔符行错误 [英] UISegmentedControl with custom color: separator line bug

查看:90
本文介绍了具有自定义颜色的UISegmentedControl:分隔符行错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

它很容易改变UISegmentedControl的颜色。我找到了各种解决方案,如这个这个网站和最好的此解决方案。但没有一个是我想要的。

Its easy change the color of UISegmentedControl. I found various solution like this, this site and the best this solution. But none was what I want.

我尝试创建一个简单的东西,它很容易,这是我的代码:(我使用的是iOS 4.2,而不是5.0和xcode 4.0.2)

I tried create a simple thing and it work very easy, this was my code: (I am using the iOS 4.2, not the 5.0 and xcode 4.0.2)

id segment[3];
UISegmentedControl *segmentedControl;   
- (id)init
{
    NSArray *itens = [NSArray arrayWithObjects: @"Option 1", @"Option 2", @"Option 3", nil];    
    segmentedControl = [[UISegmentedControl alloc] initWithItems:itens];
    [segmentedControl setFrame:CGRectMake(0, 0, 500, 30)];
    [segmentedControl setSegmentedControlStyle:UISegmentedControlStyleBar];
    [segmentedControl addTarget:self 
                    action:@selector(segmentedControl:) 
          forControlEvents:UIControlEventAllEvents];

    switch (type) {
        case type1: [segmentedControl setSelectedSegmentIndex:0]; break;
        case type2: [segmentedControl setSelectedSegmentIndex:1]; break;        
        case type3: [segmentedControl setSelectedSegmentIndex:2]; break;        
    }
    for (int i=0; i<3; i++) {
        //The most important trick to work, have to retain the subviews
        segment[i] = [[[segmentedControl subviews] objectAtIndex:i] retain];
    }
    [self changeColor];
    [self addSubview:segmentedControl];
    return self;
}  

- (void)segmentedControl:(id)sender 
{
    //do some thing
    [self changeColor];
}

- (void)changeColor{ 
    for (int i=0; i<3; i++) {
        [segment[i] setTintColor:[UIColor lightGrayColor]];
    }
    int select = segmentedControl.selectedSegmentIndex;
    [segment[select] setTintColor:[UIColor blueColor]];     
}

所以它创造了这个:

非常好,然后我点击选项2

哇,这是我想要的,所以点击选项3

Wow, this is exacly what I want, so click in Option 3

现在问题是,这个愚蠢的蓝线(红色标记)在选项1 选项2之间。如果我再次点击选项1 ,我将会:

Now the problem, this stupid blue line (marked in red square) between Option 1 and Option 2. If I click in Option 1 again, I will have:

再次出现蓝线。这意味着旧点击片段的每个左侧(但不包括第一个片段)将具有此蓝线。如果我从右到左不会发生。

Than the blue line appear again. This mean that every left side on old clicked segment (but not with the first) will have this blue line. If I go from right to left it not happens.

我不知道如何解决这个问题。如何访问此行并更改颜色?或者我将不得不使用其他代码。也许他们会遇到同样的问题...

I have no idea how to solve this. How can I access this line and change your color? Or I will have to use other codes. Maybe they will have the same problem...

推荐答案

哇...当我们写的是stackoverflow时,我们可以冷静下来并且想得更好。我在我的问题中写下答案:

Wow... When we write were in stackoverflow, we can calm down and think better. I write the answer in my ow question:

如果我从右到左,它就不会发生。

这是解决方案!我该怎么办?模拟这个。

This is the solution! What I have to do? Simulate this.

BOOL inside = FALSE;
- (void)changeColor{ 
    int old = segmentedControl.selectedSegmentIndex;
    for (int i=0; i<3; i++) {
      [segment[i] setTintColor:[UIColor lightGrayColor]];
    }  
/**/inside = TRUE;
/**/for (int i=3; i>0; i--) {
/**/     //When I do this, it call "segmentedControl:"
/**/    [segmentedControl setSelectedSegmentIndex:i];
/**/}

/**/int select = old;
    [segment[select] setTintColor:[UIColor blueColor]];     
    [segmentedControl setSelectedSegmentIndex:select];    
/**/inside = FALSE;    
} 

- (void)segmentedControl:(id)sender 
{
/**/if (inside==TRUE) return;   //Ignore this calls.
    //do some thing
    [self changeColor];
}

这篇关于具有自定义颜色的UISegmentedControl:分隔符行错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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