2011-11-04 31 views
5

Dễ thay đổi màu sắc của UISegmentedControl. Tôi đã tìm thấy các giải pháp khác nhau như this, this site và tốt nhất là this solution. Nhưng không có gì tôi muốn.UISegmentedControl với màu tùy chỉnh: lỗi dòng phân tách

tôi đã cố gắng tạo ra một điều đơn giản và nó hoạt động rất dễ dàng, đây là mã của tôi: (Tôi đang sử dụng iOS 4.2, không phải là 5.0 và 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]];  
} 

Vì vậy, nó tạo này:

first image

Rất tốt, sau đó nhấp vào Option 2

second

Wow, đây là exacly những gì tôi muốn, do đó nhấp vào Option 3

problem

Bây giờ vấn đề, dòng này ngu ngốc xanh (được đánh dấu trong hình vuông màu đỏ) giữa Option 1Option 2. Nếu tôi nhấp vào Option 1 lần nữa, tôi sẽ có:

boring

Thần đường màu xanh xuất hiện trở lại. Điều này có nghĩa là mỗi bên trái trên đoạn được nhấp chuột cũ (nhưng không phải với đoạn đầu tiên) sẽ có đường màu xanh lam này. Nếu tôi đi từ phải sang trái nó sẽ không xảy ra.

Tôi không biết cách giải quyết vấn đề này. Làm thế nào tôi có thể truy cập vào dòng này và thay đổi màu sắc của bạn? Hoặc tôi sẽ phải sử dụng các mã khác. Có lẽ họ sẽ có cùng một vấn đề ...

+0

Tôi đã gặp vấn đề tương tự như bạn và dành nhiều ngày để tìm sự cố. Cảm ơn một người bạn rất nhiều bạn đã cứu cuộc sống của tôi .. !!!! – chatur

Trả lời

3

Wow ... Khi chúng tôi viết là trong stackoverflow, chúng tôi có thể bình tĩnh và suy nghĩ tốt hơn. Tôi viết câu trả lời trong câu hỏi ow của tôi:

If I go from right to left it not happens.

Đây là giải pháp! Tôi phải làm gì? Mô phỏng điều này.

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]; 
} 
5

Tôi nghĩ có giải pháp dễ dàng hơn rất nhiều. Chỉ cần làm sạch các con trỏ ..

for (int i=0; i<[self.segmentedControll.subviews count]; i++) 
{ 
    [[self.segmentedControll.subviews objectAtIndex:i] setTintColor:nil]; 
    if (![[self.segmentedControll.subviews objectAtIndex:i]isSelected]) 
    { 
     UIColor *tintcolor=[UIColor blackColor]; 
     [[self.segmentedControll.subviews objectAtIndex:i] setTintColor:tintcolor]; 
    } 
    else 
    { 
     UIColor *tintcolor=[UIColor blueColor]; 
     [[self.segmentedControll.subviews objectAtIndex:i] setTintColor:tintcolor]; 
    } 
} 
+0

Ngoài ra, bạn phải thiết lập màu sắc trong viewDidApear để có điều khiển phân đoạn hiển thị màu sắc dự định khi nó tải. Giải pháp tốt và dễ dàng. – mircaea

Các vấn đề liên quan