简体   繁体   English

如何在iOS中更改选择器视图所选对象的颜色

[英]how to change the picker view selected object colour in ios

How can change the selected element colour in uipickerview. 如何在uipickerview中更改所选元素的颜色。 have 10 elements in picker view they are all black colour .when selecting an one from 1 to 9 means the selected element want to show in blue colour. 在选择器视图中有10个元素,它们都是黑色。当从1到9中选择一个元素时,表示所选元素要以蓝色显示。 but the last element selected means it want to show red colour. 但是最后选择的元素表示它要显示红色。 how can i achieve this help me. 我如何才能做到这一点对我有帮助。

- (void)viewDidLoad
{
    [super viewDidLoad];

    arrayNo = [[NSMutableArray alloc] init];

    [arrayNo addObject:@"1"];
    [arrayNo addObject:@"2"];
    [arrayNo addObject:@"3"];
    [arrayNo addObject:@"4"];
    [arrayNo addObject:@"5"];
    [arrayNo addObject:@"6"];
    [arrayNo addObject:@"7"];
    [arrayNo addObject:@"8"];
    [arrayNo addObject:@"9"];

}

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

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{

    UIView *view = [pickerView viewForRow:row forComponent:component];

    UILabel *selectedLabel = (UILabel *) [view viewWithTag:1002];

    if ([[arrayNo objectAtIndex:row] isEqual:[arrayNo lastObject]]) {
        [selectedLabel setTextColor:[UIColor redColor]];
    } else {
        [selectedLabel setTextColor:[UIColor blueColor]];
    }

}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    return [arrayNo count];
}

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

    if (view) {
        NSLog(@"view exists");
    } else {

        view = [[UIView alloc] initWithFrame:CGRectZero];
        [view setTag:1001];

        UILabel *lbl=[[UILabel alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(pickerView.frame), 30)];
        [lbl setBackgroundColor:[UIColor clearColor]];
        [lbl setFont:[UIFont boldSystemFontOfSize:16]];
        [lbl setTextAlignment:NSTextAlignmentCenter];
        [lbl setTag:1002];
        [lbl setTextColor:[UIColor blackColor]];
        [lbl setText:[arrayNo objectAtIndex:row]];

        [view addSubview:lbl];

    }


    return view;

}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
for(UIView *lview in pickerView.subviews) {
    if ([lview isKindOfClass:[UILabel class]]) {

    }
    NSLog(@"%@",lview);
}
}

By using the above code you will get all the sub view and check wheather it is label or not. 通过使用上面的代码,您将获得所有子视图并检查是否为标签。

Then try to put different color to each label and then you find to your specific label, then you can do what ever you need. 然后尝试为每个标签加上不同的颜色,然后找到特定的标签,然后您便可以执行所需的操作。

The subviews order may change depending on the number of component in the picker.So, not added the exact logic. 子视图的顺序可能会根据选择器中组件的数量而变化。因此,未添加确切的逻辑。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在Titanium中的iOS上更改选择器的字体颜色 - Change font colour of picker on iOS in Titanium CosmicMind / Material:如何在iOS中更改CheckButton选定的颜色 - CosmicMind/Material : How to change CheckButton Selected Colour in iOS 选中后如何更改集合视图单元格的背景颜色? - how to change collection view cell background colour when it is selected? iOS 9 选中 tabor 项背景颜色变化 - iOS 9 selected tabor item background colour change 如何在xCode中更改选择器文本的颜色? - How to change colour of picker text in xCode? 如何在iOS Objective C中更改日期选择器视图和选择器视图的字体大小 - how to change font size of date picker view and picker view in iOS Objective C 没有在标签上的选择器视图中显示所选对象 - Not showing the selected object from picker view on the label 如何在iOS上使用点击手势识别器来更改所选图像的颜色? - How do I change the colour of selected piece of image using tap gesture recognizer on iOS? swift - 如何在swift 3的表格视图中仅更改选定行和未选定行的字体颜色和大小? - How to change font colour and size for only selected row and for unselected rows to be normal in table view in swift 3? 在 NavigationView 中更改 Picker 的颜色 - Change colour of Picker within NavigationView
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM