欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

iOS pickerView和datePicker选择的分隔线有时显示有时不显示

程序员文章站 2022-06-07 12:28:25
...

UIPickerView

UIPickerView操作分隔线需要在代理方法中操作

//隐藏分割线
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
    ((UILabel *)[pickerView.subviews objectAtIndex:1]).hidden = YES;//隐藏分隔线
    ((UILabel *)[pickerView.subviews objectAtIndex:2]).hidden = YES;//隐藏分隔线
    return _items[row];
}
//显示分隔线
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
    ((UILabel *)[pickerView.subviews objectAtIndex:1]).backgroundColor = color;//显示分隔线
    ((UILabel *)[pickerView.subviews objectAtIndex:2]).backgroundColor = color;//显示分隔线
    return _items[row];
}

UIDatePicker

UIDatePicker可以调用下面方法来操作

//隐藏分割线
- (void)clearSpearatorLine {
    for (UIView *subView in self.datePicker.subviews) {
        if ([subView isKindOfClass:[UIPickerView class]]) {
            for (UIView *subView2 in subView.subviews) {
                if (subView2.frame.size.height < 1) {//取出分割线view
                    subView2.hidden = YES;//隐藏分割线
                }
            }
        }
    }
}
//隐藏分割线
- (void)clearSpearatorLine {
    for (UIView *subView in self.datePicker.subviews) {
        if ([subView isKindOfClass:[UIPickerView class]]) {
            for (UIView *subView2 in subView.subviews) {
                if (subView2.frame.size.height < 1) {//取出分割线view
                    subView2.backgroundColor = color;//显示分割线
                }
            }
        }
    }
}