修改PickerView选中行背景色

修改PickerView选中行背景色_第1张图片

实现图中效果,背景色为淡灰色,去掉上下两条线

创建UIPickerView的类 作为基类

//.h文件
#import 
@interface BasePickerView : UIPickerView
@end
//.m文件
#import "BasePickerView.h"

@implementation BasePickerView
{
    UIView *_selectBackView;
}

-(void)layoutSubviews
{
    [super layoutSubviews];
    if(self.subviews.count){
        [self updateSelectView];
    }
}

-(void)updateSelectView
{
    //修改线条颜色
    UIView *line1 = self.subviews[1];
    line1.backgroundColor = [UIColor clearColor];
    UIView *line2 = self.subviews[2];
    line2.backgroundColor = [UIColor clearColor];

    //修改选中行的背景色
    for (UIView *subView in self.subviews) {
        if(subView.subviews.count){
            UIView *contentView = subView.subviews[0];
            for (UIView *contentSubView in contentView.subviews) {
                if(contentSubView.center.y == contentView.center.y){
                    if(_selectBackView != contentSubView){
                        _selectBackView.backgroundColor = [UIColor clearColor];
                        _selectBackView = contentSubView;
                        _selectBackView.backgroundColor = H_CellContenViewBackColor;
                    }
                    break;
                }
            }
            break;
        }
    }
}
@end

你可能感兴趣的:(oc基础)