0

我正在尝试添加UIDatePicker一个本地String选项,该选项将代表 的选项Ongoing,例如作业开始-结束间隔。但是无法找到一些东西。

我尝试使用 and 创建一个自定义视图UIButton并将UIDatePicker其用作inputViewfor UITextField,但是当我点击它时,UITextField它变成了响应者并且没有输入视图。

class DatePickerView: UIView {
    
    private lazy var datePicker: UIDatePicker = {
        let picker = UIDatePicker()
        picker.datePickerMode = .date
        picker.translatesAutoresizingMaskIntoConstraints = false
        return picker
    }()
    
    private lazy var ongoingButton: UIButton = {
        let button = UIButton(type: .system)
        button.backgroundColor = .clear
        button.setTitleColor(.blue, for: .normal)
        button.setTitle("Ongoing", for: .normal)
        button.translatesAutoresizingMaskIntoConstraints = false
        button.titleLabel?.font = .systemFont(ofSize: 18)
        return button
    }()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        addSubview(ongoingButton)
        addSubview(datePicker)
        
        ongoingButton.topAnchor.constraint(equalTo: topAnchor).isActive = true
        ongoingButton.leadingAnchor.constraint(equalTo: leadingAnchor).isActive = true
        ongoingButton.trailingAnchor.constraint(equalTo: trailingAnchor).isActive = true
        ongoingButton.heightAnchor.constraint(equalToConstant: 44).isActive = true
        
        datePicker.topAnchor.constraint(equalTo: ongoingButton.bottomAnchor).isActive = true
        datePicker.leadingAnchor.constraint(equalTo: leadingAnchor).isActive = true
        datePicker.trailingAnchor.constraint(equalTo: trailingAnchor).isActive = true
        datePicker.bottomAnchor.constraint(equalTo: bottomAnchor).isActive = true
        
        heightAnchor.constraint(equalToConstant: 200).isActive = true
    }
    
    required init?(coder: NSCoder) {
        fatalError()
    }
    
}

有什么建议么?

4

0 回答 0