UIToolbar+Extension.swift 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. //
  2. // UIToolbar+Extension.swift
  3. // Nextcloud
  4. //
  5. // Created by Henrik Storch on 18.03.22.
  6. // Copyright © 2022 Henrik Storch. All rights reserved.
  7. //
  8. // Author Henrik Storch <henrik.storch@nextcloud.com>
  9. //
  10. // This program is free software: you can redistribute it and/or modify
  11. // it under the terms of the GNU General Public License as published by
  12. // the Free Software Foundation, either version 3 of the License, or
  13. // (at your option) any later version.
  14. //
  15. // This program is distributed in the hope that it will be useful,
  16. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. // GNU General Public License for more details.
  19. //
  20. // You should have received a copy of the GNU General Public License
  21. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. //
  23. import UIKit
  24. extension UIToolbar {
  25. static func toolbar(onClear: (() -> Void)?, onDone: @escaping () -> Void) -> UIToolbar {
  26. let toolbar = UIToolbar()
  27. toolbar.sizeToFit()
  28. var buttons: [UIBarButtonItem] = []
  29. if let onClear = onClear {
  30. let clearButton = UIBarButtonItem(title: NSLocalizedString("_clear_", comment: ""), style: .plain) {
  31. onClear()
  32. }
  33. buttons.append(clearButton)
  34. }
  35. buttons.append(UIBarButtonItem(barButtonSystemItem: UIBarButtonItem.SystemItem.flexibleSpace, target: nil, action: nil))
  36. let doneButton = UIBarButtonItem(title: NSLocalizedString("_done_", comment: ""), style: .done) {
  37. onDone()
  38. }
  39. buttons.append(doneButton)
  40. toolbar.setItems(buttons, animated: false)
  41. return toolbar
  42. }
  43. // by default inputAccessoryView does not respect safeArea
  44. var wrappedSafeAreaContainer: UIView {
  45. let view = InputBarWrapper()
  46. view.addSubview(self)
  47. self.translatesAutoresizingMaskIntoConstraints = false
  48. NSLayoutConstraint.activate([
  49. self.topAnchor.constraint(equalTo: view.topAnchor),
  50. self.leftAnchor.constraint(equalTo: view.leftAnchor),
  51. self.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor),
  52. self.rightAnchor.constraint(equalTo: view.rightAnchor)
  53. ])
  54. return view
  55. }
  56. }
  57. // https://stackoverflow.com/a/67985180/9506784
  58. class InputBarWrapper: UIView {
  59. var desiredHeight: CGFloat = 0 {
  60. didSet { invalidateIntrinsicContentSize() }
  61. }
  62. override var intrinsicContentSize: CGSize { CGSize(width: 0, height: desiredHeight) }
  63. required init?(coder aDecoder: NSCoder) { fatalError() }
  64. override init(frame: CGRect) {
  65. super.init(frame: frame)
  66. autoresizingMask = .flexibleHeight
  67. }
  68. }