How to change color of selected row in UIPickerView

前端 未结 10 788
孤独总比滥情好
孤独总比滥情好 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:03

    Solved! Declare 2 instance variables: selectedView, and oldView. Then the following code does the trick:

    if (self.oldView != nil)
            self.oldView.backgroundColor = [UIColor clearColor];
    
    self.selectedView = [picker viewForRow:row forComponent:kNumberComponent];
            self.selectedView.backgroundColor = [UIColor redColor];
            [self.selectedView setNeedsDisplay];
            self.oldView = self.selectedView;
    
    0 讨论(0)
  • 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]];
    
    }
    
    0 讨论(0)
  • 2020-12-03 11:08

    // CGRectMake values for the frame we’d like

    UIPickerView *myPickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0,0, self.view.bounds.size.width, self.view.bounds.size.height)];
    
    // add the UIPickerView to your viewcontroller [mainView addSubview:myPickerView];
    
    // set the selectionindicator to none myPickerView.showsSelectionIndicator = NO;
    

    // define the image that we would like to use

    UIImage *selectorImage = [UIImage imageNamed:@"selectionIndicator.png"];
    
    UIView *customSelector = [[UIImageView alloc] initWithImage:selectorImage];
    

    // set the x and y values to the point on the UIPickerView where you want to place the image

    // set the width and height values to the width and height of your image

    customSelector.frame = CGRectMake(10,(myPickerView.bounds.size.height / 2) + 16, self.view.bounds.size.width – 10, 47);
    

    // add the custom selectionIndicator also to the same viewcontroller

    [mainView addSubview:customSelector];
    
    0 讨论(0)
  • 2020-12-03 11:12

    The correct format for viewForPicker is:

    - (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
    {
        UILabel *label = (UILabel*) view;
        if (label == nil)
        {
            label = [[UILabel alloc] init];
        }
    
        [label setText:@"Whatever"];
    
        // This part just colorizes everything, since you asked about that.
    
        [label setTextColor:[UIColor whiteColor]];
        [label setBackgroundColor:[UIColor blackColor]];
        CGSize rowSize = [pickerView rowSizeForComponent:component];
        CGRect labelRect = CGRectMake (0, 0, rowSize.width, rowSize.height);
        [label setFrame:labelRect];
    
        return label;
    }
    

    The problem with the code above is: it colorizes the labels, but not the picker, itself. So, when you roll to one end or the other, there's a blank spot where you can see the white background. Setting [myPicker setBackgroundColor...] doesn't do what one might hope.

    0 讨论(0)
  • 2020-12-03 11:13

    It's unclear where you are putting the above code. Is it in -pickerView:viewForRow:forComponent:reusingView:? This is where it should be. Are you sure that you are maintaining a pointer to the label for that particular view? The fact that you are crashing suggests you probably are not. A larger block of code, including where you have put it, would be helpful.

    0 讨论(0)
  • 2020-12-03 11:14

    Here's what worked for me:

    1. Add a UIView as a subview to your UIPickerView
    2. Constrain it to be Y entered with your UIPickerView and the has the same width
    3. Give it height equal to the height of what returned in pickerView(_ pickerView: UIPickerView, rowHeightForComponent component: Int) -> CGFloat
    4. Make it semi transparent and color it the way you want

    *the rest of the solutions (with custom views for row or attributed stringsfor row) were buggy when scrolling fast.

    0 讨论(0)
提交回复
热议问题