Show UIPickerView like a keyboard, without UITextField

后端 未结 6 819
长发绾君心
长发绾君心 2021-02-05 15:56

I\'m looking for a way to present a UIPickerView when the user taps on a UIBarButtonItem. Imagine a filter for the table view results.

I know I

6条回答
  •  花落未央
    2021-02-05 16:38

    The cleanest way at least for me, and for Swift4 + Autolayout solution, without a UITextField is adding a UIView as a container of the UIPickerView and a UIToolbar for the top views/buttons.

    Then toggle the inset/offset of that container view with animation if you want, to hide and unhide the picker.

    PROPERTIES

    private lazy var view_PickerContainer: UIView = {
        let view = UIView()
        view.backgroundColor = .white
        view.addSubview(self.timePicker)
        view.addSubview(self.toolbar_Picker)
        return view
    }()
    
    private lazy var timePicker: UIDatePicker = {
        let picker = UIDatePicker()
        picker.minimumDate = Date()
        picker.datePickerMode = .time
        picker.setDate(Date(), animated: true)
        return picker
    }()
    
    private let timeFormatter: DateFormatter = {
        let formatter = DateFormatter()
        formatter.dateFormat = "hh:mm a"
        return formatter
    }()
    
    private lazy var toolbar_Picker: UIToolbar = {
        let toolbar = UIToolbar()
        toolbar.barStyle = .blackTranslucent
        toolbar.barTintColor = .blueDarkText
        toolbar.tintColor = .white
        self.embedButtons(toolbar)
        return toolbar
    }()
    

    I use SnapKit to layout my views programmatically. You can use the timePicker's bounds.size as reference for your layout.

    IMPLEMENTATION

    In viewDidLoad()

        // Setup timepicker and its container.
        self.view.addSubview(self.view_PickerContainer)
        self.view_PickerContainer.snp.makeConstraints { (make) in
            make.height.equalTo(self.timePicker.bounds.size.height + 50.0)
            make.leading.trailing.equalToSuperview()
            self.constraint_PickerContainerBottom = make.bottom.equalToSuperview().inset(-500.0).constraint
        }
    
        self.timePicker.snp.makeConstraints { (make) in
            make.height.equalTo(self.timePicker.bounds.size.height)
            make.width.equalToSuperview()
            make.bottom.equalToSuperview()
        }
    
        // Add toolbar for buttons.
        self.toolbar_Picker.snp.makeConstraints { (make) in
            make.height.equalTo(40.0)
            make.top.leading.trailing.equalToSuperview()
        }
    

    Embedding top views

    private func embedButtons(_ toolbar: UIToolbar) {
        func setupLabelBarButtonItem() -> UIBarButtonItem {
            let label = UILabel()
            label.text = "Set Alarm Time"
            label.textColor = .white
            return UIBarButtonItem(customView: label)
        }
    
        let todayButton = UIBarButtonItem(title: "Today", style: .plain, target: self, action: #selector(self.todayPressed(_:)))
    
        let doneButton = UIBarButtonItem(barButtonSystemItem: .done, target: self, action: #selector(self.donePressed(_:)))
    
        let flexButton = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: self, action: nil)
    
        toolbar.setItems([todayButton, flexButton, setupLabelBarButtonItem(), flexButton, doneButton], animated: true)
    }
    

    And the result looks like this:

提交回复
热议问题