iosobjective-cuiviewuipickerviewuipickerviewdelegate

Static view for center row in a UIPickerView


I have a pickerView to which I add a custom view to each row by viewForRow delegate method. The goal here is to have a different view for the center(selected) row like a highlight view WHILE the pickerView spins (a center static view). Any help would be appreciated..


Solution

  • In iOS 7, you can do this adding a subview to your UIPickerView. In earlier versions, it will be such a painful operation because UIPickerView will have a border etc, so probably you will need a custom picker-like implementation.

    For iOS 7, you can use something like below:

    CGFloat rowHeight = 100;  // rowHeight of your picker view, i assumed your rowHeight equals to 100
    
    // Create a custom view, centered vertically in your picker view
    UIView * view = [[UIView alloc] initWithFrame:CGRectMake(0,
                                                             (pickerView.bounds.size.height - rowHeight)/2,
                                                             pickerView.bounds.size.width,
                                                             rowHeight)];
    [view setBackgroundColor:[UIColor clearColor]];
    
    // Do some highlighting operations
    [view.layer setCornerRadius:5.0];
    [view.layer setBorderColor:[UIColor blueColor].CGColor];
    [view.layer setBorderWidth:5.0];
    
    [pickerView addSubview:view];