UIPickerView 设置上下2根横线颜色OC代码 颜色
可以使用UIPickerViewDelegate的方法viewForRow:forComponent:reusingView:来自定义UIPickerView的每一行的视图,然后在视图中添加两根横线,设置它们的颜色。
首先,需要实现UIPickerViewDelegate协议,然后在代理方法viewForRow:forComponent:reusingView:中自定义每一行的视图。
示例代码如下:
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(nullable UIView *)view {
// 创建自定义的视图
UIView *customView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, pickerView.bounds.size.width, 44)];
// 创建上横线
UIView *topLine = [[UIView alloc] initWithFrame:CGRectMake(0, 0, pickerView.bounds.size.width, 1)];
topLine.backgroundColor = [UIColor redColor]; // 设置颜色
[customView addSubview:topLine];
// 创建下横线
UIView *bottomLine = [[UIView alloc] initWithFrame:CGRectMake(0, customView.bounds.size.height - 1, pickerView.bounds.size.width, 1)];
bottomLine.backgroundColor = [UIColor blueColor]; // 设置颜色
[customView addSubview:bottomLine];
// 添加其他自定义内容
return customView;
}
在上面的代码中,我们创建了两个UIView对象作为上下横线,并设置了它们的颜色。然后将它们添加到自定义视图中,最后返回自定义视图作为每一行的视图。
你可以根据需要设置上下横线的颜色,代码中的示例颜色为红色和蓝色
原文地址: https://www.cveoy.top/t/topic/iiIA 著作权归作者所有。请勿转载和采集!