对UIPickerView进行自定义,下面是改变其分割线颜色的方法,原理就是找到UIPickerView的子View高度小于1的View,然后改变线的颜色,即可实现,此外在
iOS10下分割线颜色默认是透明的。
注意:这个方法只有放到下面的方法才有效果,获取
pickerView:viewForRow:forComponent:reusingView:
中定义的View,当
pickerView:viewForRow:forComponent:reusingView:
未实现或者行或分组不可用时返回nil。
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view {
[self changeSpearatorLineColor];
}
#pragma mark - 改变分割线的颜色
- (void)changeSpearatorLineColor
{
for(UIView *speartorView in picker.subviews)
{
if (speartorView.frame.size.height < 1)//取出分割线view
{
// 本人做法
UIView *view =[UIView alloc]initWithFrame:CGRectMake(0,0,k_SCREEN_WIDTH,0.667)];
view.backgroundColor = [UIColor colorWithDisplayP3Red:0.5 green:0.5 blue:0.5 alpha:1.0];// iOS10 sRGB
[speartorView addSubView:view];
// 此方法不可行,pickView 整个会被渲染;
speartorView.backgroundColor = [UIColor colorWithDisplayP3Red:0.5 green:0.5 blue:0.5 alpha:1.0];// iOS10 sRGB 渲染
}
}
}