ToolbarPickerView.swift 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. //
  2. // ToolbarPickerView.swift
  3. // Learn Genie
  4. //
  5. // Created by Suraj Kumar Mandal on 17/09/21.
  6. //
  7. import Foundation
  8. import UIKit
  9. protocol ToolbarPickerViewDelegate: AnyObject {
  10. func didTapDone()
  11. func didTapCancel()
  12. }
  13. class ToolbarPickerView: UIPickerView {
  14. public private(set) var toolbar: UIToolbar?
  15. public weak var toolbarDelegate: ToolbarPickerViewDelegate?
  16. override init(frame: CGRect) {
  17. super.init(frame: frame)
  18. self.commonInit()
  19. }
  20. required init?(coder aDecoder: NSCoder) {
  21. super.init(coder: aDecoder)
  22. self.commonInit()
  23. }
  24. private func commonInit() {
  25. let toolBar = UIToolbar()
  26. toolBar.barStyle = UIBarStyle.default
  27. //toolBar.isTranslucent = true
  28. //toolBar.tintColor = .black
  29. toolBar.sizeToFit()
  30. let doneButton = UIBarButtonItem(title: "Done", style: .done, target: self, action: #selector(self.doneTapped))
  31. let spaceButton = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: nil, action: nil)
  32. let cancelButton = UIBarButtonItem(title: "Cancel", style: .plain, target: self, action: #selector(self.cancelTapped))
  33. toolBar.setItems([cancelButton, spaceButton, doneButton], animated: false)
  34. toolBar.isUserInteractionEnabled = true
  35. self.toolbar = toolBar
  36. }
  37. @objc func doneTapped() {
  38. self.toolbarDelegate?.didTapDone()
  39. }
  40. @objc func cancelTapped() {
  41. self.toolbarDelegate?.didTapCancel()
  42. }
  43. }