默认的“UIPickerView”在我想删除的顶部和底部有一条细灰线,但我无法知道如何做到这一点。
有什么地方我可以设置清除吗?
你必须继承UIPickerView
,因此:
class MyPickerView: UIPickerView {
override func layoutSubviews() {
super.layoutSubviews()
self.layer.borderWidth = 0 // Main view rounded border
// Component borders
self.subviews.forEach {
$0.layer.borderWidth = 0
$0.isHidden = $0.frame.height <= 1.0
}
}
}
在picker视图数据源方法中
func numberOfComponents(in pickerView: UIPickerView) -> Int {
pickerView.subviews.forEach({
$0.isHidden = $0.frame.height < 1.0
})
return 1
}
线条将消失
我认为您需要删除所选行的两行,如果是这样,那么您可以使用pickerView的titleForRow或viewForRow委托方法,并在委托方法中使用以下代码。
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
[[pickerView.subviews objectAtIndex:1] setHidden:TRUE];
[[pickerView.subviews objectAtIndex:2] setHidden:TRUE];
}
要么
- (UIView *)pickerView:(UIPickerView *)pickerView
viewForRow:(NSInteger)row
forComponent:(NSInteger)component
reusingView:(UIView *)view
{
[[pickerView.subviews objectAtIndex:1] setHidden:TRUE];
[[pickerView.subviews objectAtIndex:2] setHidden:TRUE];
}