它很容易改變UISegmentedControl的顏色。我發現了各種解決方案,如this,this site和最好的this solution。但沒有一個是我想要的。UISegmentedControl自定義顏色:分隔線錯誤
我試圖創建一個簡單的事情,它的工作很容易,這是我的代碼:(我使用的是iOS 4.2的,不是5.0的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]];
}
因此,創建此:
很好,然後我點擊Option 2
哇,這是exacly我想要的東西,在Option 3
現在,所以點擊這個問題,Option 1
和Option 2
之間的這個愚蠢的藍線(標記爲紅色方塊)。如果我在Option 1
再次點擊,我將有:
比藍線再次出現。這意味着舊點擊段(但不是第一個點)上的每個左側都會有這條藍線。如果我從右向左走,它不會發生。
我不知道如何解決這個問題。我如何訪問這一行並更改顏色?或者我將不得不使用其他代碼。也許他們會有同樣的問題...
我和你有同樣的問題,並花了幾天的時間來找到問題。感謝很多朋友你拯救了我的生命.. !!!! – chatur