2014-06-30 40 views
-1

所以我想要的是在PickerView中爲我的行製作單獨的顏色,但我真的不知道如何做到這一點。知道如何製作個性化的文字顏色也很酷。 謝謝。PickerView爲行設置單獨的顏色

+0

你到目前爲止嘗試過什麼嗎?如果是這樣,你有什麼嘗試? –

回答

1

所以,如果你正在檢查的UIPickerView datasource,你會發現下面的方法:

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view; 

我想你可以通過處理像這樣使用它,以修改視圖在pickerView

  • 啓動您的pickerView

在你viewDidLoad爲例如:

UIPickerView *pickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, self.view.frame.size.height/4, self.view.frame.size.width, self.view.frame.size.height/2)]; 
pickerView.delegate = self; 
pickerView.dataSource = self; 

[self.view addSubview:pickerView]; 
  • 調用所需的方法並進行更改:

例子:

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view 
{ 
UIView *customRow = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 30)]; 
switch (row) { 
    case 0: 
     customRow.backgroundColor = [UIColor redColor]; 
     return customRow; 
     break; 

    case 1: 
     customRow.backgroundColor = [UIColor orangeColor]; 
     return customRow; 

    case 2: 
     customRow.backgroundColor = [UIColor yellowColor]; 
     return customRow; 

    case 3: 
     customRow.backgroundColor = [UIColor greenColor]; 
     return customRow; 

    case 4: 
     customRow.backgroundColor = [UIColor blueColor]; 
     return customRow; 

    case 5: 
     customRow.backgroundColor = [UIColor purpleColor]; 
     return customRow; 

    default: 
     return nil; 
     break; 
    } 
} 
  • 不要在執行忘記所需的方法:

在你的控制器中:

#pragma mark: UIPickeView datasource 

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView 
{ 
    return 1; 
} 

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component 
{ 
    return 6; 
}