[英]UIPickerView Row Color
有谁知道如何在iPhone SDK的UIPickerView控件中更改行(或行背景)的颜色? 与下面的标题类似,但是我也想更改行的颜色:
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component;
谢谢。
谢谢诺亚,这正是我所需要的。 我想在这里添加代码,以防万一其他人需要(或想评论:)
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view {
CGRect imageFrame = CGRectMake(0.0, 0.0, 15, 15);
UIImageView *label = [[[UIImageView alloc] initWithFrame:imageFrame] **autorelease**];
if (row == 0)
{
label.backgroundColor = [UIColor redColor];
}
if (row == 1)
{
label.backgroundColor = [UIColor blueColor];
}
if (row == 2)
{
label.backgroundColor = [UIColor blackColor];
}
return label;
}
您可以在委托的-pickerView:viewForRow:forComponent:reusingView:方法中返回任意视图,在此处记录 。
我根据肖恩的答案实现了以下内容:
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
{
CGRect rowFrame = CGRectMake(00.0f, 0.0f, [pickerView viewForRow:row forComponent:component].frame.size.width, [pickerView viewForRow:row forComponent:component].frame.size.height);
UILabel *label = [[UILabel alloc] initWithFrame:rowFrame];
label.font = [UIFont boldSystemFontOfSize:18.0f];
// This is an array I pass to the picker in prepareForSegue:sender:
label.text = [self.values objectAtIndex:row];
label.textAlignment = UITextAlignmentCenter;
// This is an array I pass to the picker in prepareForSegue:sender:
if ([self.backgroundColors count]) {
label.backgroundColor = [self.backgroundColors objectAtIndex:row];
// self.lightColors is an array I instantiate in viewDidLoad: self.lightColors = @[ [UIColor yellowColor], [UIColor greenColor], [UIColor whiteColor] ];
label.textColor = [self.lightColors containsObject:label.backgroundColor] ? [UIColor blackColor] : [UIColor whiteColor];
} else {
label.textColor = [UIColor blackColor];
}
return label;
}
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
UILabel *labelSelected = (UILabel*)[pickerView viewForRow:row forComponent:component];
[labelSelected setTextColor:[UIColor redColor]];
}
和
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view{
UILabel *label = (id)view;
if (!label){
label=[[UILabel alloc]init];
label.textAlignment = NSTextAlignmentCenter;
pickerView.backgroundColor=[UIColor whiteColor];
label.text=[self pickerView:pickerView titleForRow:row forComponent:component];
label.textColor=[UIColor grayColor];
}
return label;
}
做这个:
label.backgroundColor = [UIColor yourcolorColor];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.