UIToolbar+Extension.swift 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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 Foundation
  24. import UIKit
  25. extension UIToolbar {
  26. static func toolbar(onClear: (() -> Void)?, onDone: @escaping () -> Void) -> UIToolbar {
  27. let toolbar = UIToolbar()
  28. toolbar.sizeToFit()
  29. var buttons: [UIBarButtonItem] = []
  30. if let onClear = onClear {
  31. let clearButton = UIBarButtonItem(title: NSLocalizedString("_clear_", comment: ""), style: .plain) {
  32. onClear()
  33. }
  34. buttons.append(clearButton)
  35. }
  36. buttons.append(UIBarButtonItem(barButtonSystemItem: UIBarButtonItem.SystemItem.flexibleSpace, target: nil, action: nil))
  37. let doneButton = UIBarButtonItem(title: NSLocalizedString("_done_", comment: ""), style: .done) {
  38. onDone()
  39. }
  40. buttons.append(doneButton)
  41. toolbar.setItems(buttons, animated: false)
  42. return toolbar
  43. }
  44. // by default inputAccessoryView does not respect safeArea
  45. var wrappedSafeAreaContainer: UIView {
  46. let view = InputBarWrapper()
  47. view.addSubview(self)
  48. self.translatesAutoresizingMaskIntoConstraints = false
  49. NSLayoutConstraint.activate([
  50. self.topAnchor.constraint(equalTo: view.topAnchor),
  51. self.leftAnchor.constraint(equalTo: view.leftAnchor),
  52. self.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor),
  53. self.rightAnchor.constraint(equalTo: view.rightAnchor)
  54. ])
  55. return view
  56. }
  57. }
  58. // https://stackoverflow.com/a/67985180/9506784
  59. class InputBarWrapper: UIView {
  60. var desiredHeight: CGFloat = 0 {
  61. didSet { invalidateIntrinsicContentSize() }
  62. }
  63. override var intrinsicContentSize: CGSize { CGSize(width: 0, height: desiredHeight) }
  64. required init?(coder aDecoder: NSCoder) { fatalError() }
  65. override init(frame: CGRect) {
  66. super.init(frame: frame)
  67. autoresizingMask = .flexibleHeight
  68. }
  69. }