首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >UIPickerView行颜色

UIPickerView行颜色
EN

Stack Overflow用户
提问于 2008-11-22 02:48:52
回答 5查看 12.5K关注 0票数 8

有人知道如何在iPhone SDK中更改UIPickerView控件中的行(或行背景)的颜色吗?类似于下面的行标题,但是我也想更改行的颜色:

代码语言:javascript
运行
复制
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component;

谢谢。

EN

回答 5

Stack Overflow用户

回答已采纳

发布于 2008-11-22 03:18:44

您可以在委托的-pickerView:viewForRow:forComponent:reusingView:方法中返回任意视图,文档中记录了here

票数 10
EN

Stack Overflow用户

发布于 2008-11-22 04:33:13

谢谢诺亚,这正是我需要的。我想在这里添加代码,以防其他人需要(或想要评论:)

代码语言:javascript
运行
复制
- (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;
}
票数 14
EN

Stack Overflow用户

发布于 2012-08-24 06:27:03

我根据Sean的回答实现了以下内容:

代码语言:javascript
运行
复制
- (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;
}
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/310786

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档