【问题标题】:PickerView make individual colors for rowsPickerView 为行制作单独的颜色
【发布时间】:2014-08-21 04:46:24
【问题描述】:

所以我想要的是在 PickerView 中为我的行制作单独的颜色,但我真的不知道该怎么做。知道如何制作单独的文本颜色也很酷。 谢谢。

【问题讨论】:

  • 到目前为止你有没有尝试过?如果是这样,您尝试过什么?

标签: ios text colors uipickerview uicolor


【解决方案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;
}

【讨论】:

    猜你喜欢
    • 2012-04-15
    • 1970-01-01
    • 1970-01-01
    • 2021-12-16
    • 1970-01-01
    • 2021-04-05
    • 1970-01-01
    • 2019-01-02
    • 1970-01-01
    相关资源
    最近更新 更多