Ask Your Question
3

Can you develop a "done" button for RxPickerViewAttributedStringAdapter?

asked 2022-12-10 11:00:00 +0000

david gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
3

answered 2021-09-27 18:00:00 +0000

lalupa gravatar image

Yes, a "done" button can be developed for RxPickerViewAttributedStringAdapter. One possible approach for implementing this functionality is to add a tool bar above the picker view, which contains a "Done" button.

To implement this, first create the tool bar and add the "Done" button to it. Then, add the tool bar to the view hierarchy above the picker view. Finally, when the "Done" button is tapped, hide the tool bar and notify the RxPickerViewAttributedStringAdapter that the selection has been completed.

Here is an example implementation:

// Create the tool bar with a "Done" button
let toolBar = UIToolbar(frame: CGRect(x: 0, y: 0, width: view.frame.width, height: 44))
let doneButton = UIBarButtonItem(title: "Done", style: .done, target: self, action: #selector(didTapDoneButton))
toolBar.setItems([UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: nil, action: nil), doneButton], animated: false)

// Add the tool bar above the picker view
let pickerView = RxPickerViewAttributedStringAdapter<String>(components: [
  ("Option 1", NSAttributedString(string: "Option 1")),
  ("Option 2", NSAttributedString(string: "Option 2")),
  ("Option 3", NSAttributedString(string: "Option 3"))
])
pickerViewSelectedValue = pickerView.rx.selectedItem.map({ $0.title }).asDriver(onErrorJustReturn: nil)
pickerView.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(pickerView)
view.addSubview(toolBar)
NSLayoutConstraint.activate([
  pickerView.topAnchor.constraint(equalTo: toolBar.bottomAnchor),
  pickerView.leadingAnchor.constraint(equalTo: view.leadingAnchor),
  pickerView.trailingAnchor.constraint(equalTo: view.trailingAnchor),
  toolBar.topAnchor.constraint(equalTo: view.topAnchor),
  toolBar.leadingAnchor.constraint(equalTo: view.leadingAnchor),
  toolBar.trailingAnchor.constraint(equalTo: view.trailingAnchor)
])

// Handle "Done" button tap
@objc func didTapDoneButton() {
  toolBar.isHidden = true
  pickerView.selectItem(pickerView.selectedRow, animated: true, notifySelection: true)
}

In this example, the tool bar is added above the picker view using Auto Layout constraints. The RxPickerViewAttributedStringAdapter is created as usual, with its selected value being bound to a driver.

When the "Done" button is tapped, it sets the tool bar to be hidden and notifies the picker view adapter that the selection has been completed by calling the selectItem method with notifySelection set to true.

Note that this is just one possible approach for adding a "Done" button to a picker view using RxSwift. The exact implementation may vary depending on the specific requirements of the application.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2022-12-10 11:00:00 +0000

Seen: 18 times

Last updated: Sep 27 '21