NavigationAccessoryView.swift 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. // NavigationAccessoryView.swift
  2. // Eureka ( https://github.com/xmartlabs/Eureka )
  3. //
  4. // Copyright (c) 2016 Xmartlabs ( http://xmartlabs.com )
  5. //
  6. //
  7. // Permission is hereby granted, free of charge, to any person obtaining a copy
  8. // of this software and associated documentation files (the "Software"), to deal
  9. // in the Software without restriction, including without limitation the rights
  10. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. // copies of the Software, and to permit persons to whom the Software is
  12. // furnished to do so, subject to the following conditions:
  13. //
  14. // The above copyright notice and this permission notice shall be included in
  15. // all copies or substantial portions of the Software.
  16. //
  17. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  20. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  23. // THE SOFTWARE.
  24. import Foundation
  25. import UIKit
  26. public protocol NavigationAccessory {
  27. var doneClosure: (() -> ())? { get set }
  28. var nextClosure: (() -> ())? { get set }
  29. var previousClosure: (() -> ())? { get set }
  30. var previousEnabled: Bool { get set }
  31. var nextEnabled: Bool { get set }
  32. }
  33. /// Class for the navigation accessory view used in FormViewController
  34. open class NavigationAccessoryView: UIToolbar, NavigationAccessory {
  35. open var previousButton: UIBarButtonItem!
  36. open var nextButton: UIBarButtonItem!
  37. open var doneButton = UIBarButtonItem(barButtonSystemItem: .done, target: self, action: #selector(didTapDone))
  38. private var fixedSpace = UIBarButtonItem(barButtonSystemItem: .fixedSpace, target: nil, action: nil)
  39. private var flexibleSpace = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: nil, action: nil)
  40. public override init(frame: CGRect) {
  41. super.init(frame: CGRect(x: 0, y: 0, width: frame.size.width, height: 44.0))
  42. autoresizingMask = .flexibleWidth
  43. fixedSpace.width = 22.0
  44. initializeChevrons()
  45. setItems([previousButton, fixedSpace, nextButton, flexibleSpace, doneButton], animated: false)
  46. }
  47. required public init?(coder aDecoder: NSCoder) {
  48. super.init(coder: aDecoder)
  49. }
  50. private func drawChevron(pointingRight: Bool) -> UIImage? {
  51. // Hardcoded chevron size
  52. let width = 12
  53. let height = 20
  54. // Begin the image context, with which we are going to draw a chevron
  55. UIGraphicsBeginImageContextWithOptions(CGSize(width: width, height: height), false, 0.0)
  56. guard let context = UIGraphicsGetCurrentContext() else { return nil }
  57. defer {
  58. UIGraphicsEndImageContext()
  59. }
  60. // The chevron looks like > or <. This can be drawn with 3 points; the Y coordinates of the points
  61. // are independant of whether it is to be pointing left or right; the X coordinates will depend, as follows.
  62. // The 1s are to ensure that the point of the chevron does not sit exactly on the edge of the frame, which
  63. // would slightly truncate the point.
  64. let chevronPointXCoordinate, chevronTailsXCoordinate: Int
  65. if pointingRight {
  66. chevronPointXCoordinate = width - 1
  67. chevronTailsXCoordinate = 1
  68. } else {
  69. chevronPointXCoordinate = 1
  70. chevronTailsXCoordinate = width - 1
  71. }
  72. // Draw the lines and return the image
  73. context.setLineWidth(1.5)
  74. context.setLineCap(.square)
  75. context.strokeLineSegments(between: [
  76. CGPoint(x: chevronTailsXCoordinate, y: 0),
  77. CGPoint(x: chevronPointXCoordinate, y: height / 2),
  78. CGPoint(x: chevronPointXCoordinate, y: height / 2),
  79. CGPoint(x: chevronTailsXCoordinate, y: height)
  80. ])
  81. return UIGraphicsGetImageFromCurrentImageContext()
  82. }
  83. private func initializeChevrons() {
  84. var imageLeftChevron, imageRightChevron: UIImage?
  85. if #available(iOS 13.0, *) {
  86. // If we have access to SFSymbols, use the system chevron images, rather than faffing around with our own
  87. imageLeftChevron = UIImage(systemName: "chevron.left")
  88. imageRightChevron = UIImage(systemName: "chevron.right")
  89. } else {
  90. imageLeftChevron = drawChevron(pointingRight: false)
  91. imageRightChevron = drawChevron(pointingRight: true)
  92. }
  93. // RTL language support
  94. imageLeftChevron = imageLeftChevron?.imageFlippedForRightToLeftLayoutDirection()
  95. imageRightChevron = imageRightChevron?.imageFlippedForRightToLeftLayoutDirection()
  96. previousButton = UIBarButtonItem(image: imageLeftChevron, style: .plain, target: self, action: #selector(didTapPrevious))
  97. nextButton = UIBarButtonItem(image: imageRightChevron, style: .plain, target: self, action: #selector(didTapNext))
  98. }
  99. open override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {}
  100. public var doneClosure: (() -> ())?
  101. public var nextClosure: (() -> ())?
  102. public var previousClosure: (() -> ())?
  103. @objc private func didTapDone() {
  104. doneClosure?()
  105. }
  106. @objc private func didTapNext() {
  107. nextClosure?()
  108. }
  109. @objc private func didTapPrevious() {
  110. previousClosure?()
  111. }
  112. public var previousEnabled: Bool {
  113. get {
  114. return previousButton.isEnabled
  115. }
  116. set {
  117. previousButton.isEnabled = newValue
  118. }
  119. }
  120. public var nextEnabled: Bool {
  121. get {
  122. return nextButton.isEnabled
  123. }
  124. set {
  125. nextButton.isEnabled = newValue
  126. }
  127. }
  128. }