How to change color of selected row in UIPickerView

前端 未结 10 915
孤独总比滥情好
孤独总比滥情好 2020-12-03 10:25

Ok, maybe I\'m missing something really simple and I apologize if that\'s the case, however, I\'ve googled every permutation of the title and have not found! So this is sim

10条回答
  •  情歌与酒
    2020-12-03 11:04

    Normally, I use this method:

    I use the custom view for show the row item

    -(UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
    {
    
        UILabel *label = (id)view;
    
        if (!label)
        {
    
            label= [[UILabel alloc] initWithFrame:CGRectMake(0.0f, 0.0f, [pickerView rowSizeForComponent:component].width, [pickerView rowSizeForComponent:component].height)];
            label.textAlignment = NSTextAlignmentCenter;
            label.textColor = [UIColor whiteColor];
            label.text = _arrayStringPicker[row];
    
        }  
    
        return label;
    

    I change color of row selected with:

    - (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
    {
    
        UILabel *labelSelected = (UILabel*)[pickerView viewForRow:row forComponent:component];
        [labelSelected setTextColor:[UIColor redColor]];
    
    }
    

提交回复
热议问题