2015-04-25 15 views
5

Tôi gặp sự cố khi nhận UISegmentedControl để hiển thị màu sắc mong muốn.UISegmentedControl tintColor

// AppDelegate 
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { 
    // need red tint color in other views of the app 
    [[UIView appearance] setTintColor:[UIColor redColor]]; 
    return YES; 
} 

// ViewController 
- (void)viewDidLoad { 
    [super viewDidLoad]; 
    NSArray *items = @[@"Item 1", @"Item 2"]; 
    UISegmentedControl *control = [[UISegmentedControl alloc] initWithItems:items]; 
    // would like to have this control to have a green tint color 
    control.tintColor = [UIColor greenColor]; 
    [self.view addSubview:control]; 
} 

Làm cách nào để UISegmentedControl sử dụng màu xanh lục?

+0

bạn đã thử này: '[[UISegmentedControl xuất hiện] setTintColor: [UIColor greenColor]];' – BoilingLime

+0

Vâng, buồn bã tạo ra cùng một kết quả. –

+1

Không phải là cách tốt nhất để làm điều đó nhưng có thể hoạt động. cố gắng thiết lập màu cho từng phần phụ của 'UISegmentControl'? – BoilingLime

Trả lời

3

Hãy thử một cái gì đó như thế này?

for (UIView *subView in mySegmentedControl.subviews) 
{ 
    [subView setTintColor: [UIColor greenColor]]; 
} 

Nhưng nó thực sự xuất hiện rằng đó là một vấn đề được biết đến trong iOS 7, tôi không biết nếu nó đã được cố định trong iOS 8.

"Bạn không có thể tùy chỉnh phong cách điều khiển phân đoạn của trên iOS 7 . điều khiển phân đoạn chỉ có một phong cách"

UIKit User Interface Catalog

+0

Giải pháp là thêm màu sắc cho hai cấp độ của các bản xem trước (đầu tiên là UISegment, sau đó có nhãn và một số lần xem trước). Vì vậy, cảm ơn cho ý tưởng :) –

+0

Bạn đang chào đón! – BoilingLime

+0

Lạ lẫm rằng sự thay đổi xuất hiện đầu tiên hoạt động, nhưng sau đó nó được nướng. Ồ, sẽ chỉ tạo ra một loại với hành vi đúng. –

6

tôi đã kết thúc việc tạo ra một loại đối với hành vi mong muốn. Cấu trúc phân lớp trông giống như sau:

UISegment 
    UISegmentLabel 
    UIImageView 
UISegment 
    UISegmentLabel 
    UIImageView 

Vì vậy, hai vòng được yêu cầu cho hiệu ứng mong muốn (nếu không một số phần vẫn giữ nguyên màu cũ).

UISegmentedControl + TintColor.h

#import <UIKit/UIKit.h> 

@interface UISegmentedControl (TintColor) 

@end 

UISegmentedControl + TintColor.m

#import "UISegmentedControl+TintColor.h" 

@implementation UISegmentedControl (TintColor) 

- (void)setTintColor:(UIColor *)tintColor { 
    [super setTintColor:tintColor]; 
    for (UIView *subview in self.subviews) { 
     subview.tintColor = tintColor; 
     for (UIView *subsubview in subview.subviews) { 
      subsubview.tintColor = tintColor; 
     } 
    } 
} 

@end 
+0

Vẫn chưa được khắc phục vào năm 2018 ... – saeros

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