NavigationAccessoryView.swift 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. @objc(EurekaNavigationAccessoryView)
  35. open class NavigationAccessoryView: UIToolbar, NavigationAccessory {
  36. open var previousButton: UIBarButtonItem!
  37. open var nextButton: UIBarButtonItem!
  38. open var doneButton = UIBarButtonItem(barButtonSystemItem: .done, target: self, action: #selector(didTapDone))
  39. private var fixedSpace = UIBarButtonItem(barButtonSystemItem: .fixedSpace, target: nil, action: nil)
  40. private var flexibleSpace = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: nil, action: nil)
  41. public override init(frame: CGRect) {
  42. super.init(frame: CGRect(x: 0, y: 0, width: frame.size.width, height: 44.0))
  43. autoresizingMask = .flexibleWidth
  44. fixedSpace.width = 22.0
  45. initializeChevrons()
  46. setItems([previousButton, fixedSpace, nextButton, flexibleSpace, doneButton], animated: false)
  47. }
  48. required public init?(coder aDecoder: NSCoder) {
  49. super.init(coder: aDecoder)
  50. }
  51. private func drawChevron(pointingRight: Bool) -> UIImage? {
  52. // Hardcoded chevron size
  53. let width = 12
  54. let height = 20
  55. // Begin the image context, with which we are going to draw a chevron
  56. UIGraphicsBeginImageContextWithOptions(CGSize(width: width, height: height), false, 0.0)
  57. guard let context = UIGraphicsGetCurrentContext() else { return nil }
  58. defer {
  59. UIGraphicsEndImageContext()
  60. }
  61. // The chevron looks like > or <. This can be drawn with 3 points; the Y coordinates of the points
  62. // are independant of whether it is to be pointing left or right; the X coordinates will depend, as follows.
  63. // The 1s are to ensure that the point of the chevron does not sit exactly on the edge of the frame, which
  64. // would slightly truncate the point.
  65. let chevronPointXCoordinate, chevronTailsXCoordinate: Int
  66. if pointingRight {
  67. chevronPointXCoordinate = width - 1
  68. chevronTailsXCoordinate = 1
  69. } else {
  70. chevronPointXCoordinate = 1
  71. chevronTailsXCoordinate = width - 1
  72. }
  73. // Draw the lines and return the image
  74. context.setLineWidth(1.5)
  75. context.setLineCap(.square)
  76. context.strokeLineSegments(between: [
  77. CGPoint(x: chevronTailsXCoordinate, y: 0),
  78. CGPoint(x: chevronPointXCoordinate, y: height / 2),
  79. CGPoint(x: chevronPointXCoordinate, y: height / 2),
  80. CGPoint(x: chevronTailsXCoordinate, y: height)
  81. ])
  82. return UIGraphicsGetImageFromCurrentImageContext()
  83. }
  84. private func initializeChevrons() {
  85. var imageLeftChevron, imageRightChevron: UIImage?
  86. if #available(iOS 13.0, *) {
  87. // If we have access to SFSymbols, use the system chevron images, rather than faffing around with our own
  88. imageLeftChevron = UIImage(systemName: "chevron.left")
  89. imageRightChevron = UIImage(systemName: "chevron.right")
  90. } else {
  91. imageLeftChevron = drawChevron(pointingRight: false)
  92. imageRightChevron = drawChevron(pointingRight: true)
  93. }
  94. // RTL language support
  95. imageLeftChevron = imageLeftChevron?.imageFlippedForRightToLeftLayoutDirection()
  96. imageRightChevron = imageRightChevron?.imageFlippedForRightToLeftLayoutDirection()
  97. previousButton = UIBarButtonItem(image: imageLeftChevron, style: .plain, target: self, action: #selector(didTapPrevious))
  98. nextButton = UIBarButtonItem(image: imageRightChevron, style: .plain, target: self, action: #selector(didTapNext))
  99. }
  100. open override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {}
  101. public var doneClosure: (() -> ())?
  102. public var nextClosure: (() -> ())?
  103. public var previousClosure: (() -> ())?
  104. @objc private func didTapDone() {
  105. doneClosure?()
  106. }
  107. @objc private func didTapNext() {
  108. nextClosure?()
  109. }
  110. @objc private func didTapPrevious() {
  111. previousClosure?()
  112. }
  113. public var previousEnabled: Bool {
  114. get {
  115. return previousButton.isEnabled
  116. }
  117. set {
  118. previousButton.isEnabled = newValue
  119. }
  120. }
  121. public var nextEnabled: Bool {
  122. get {
  123. return nextButton.isEnabled
  124. }
  125. set {
  126. nextButton.isEnabled = newValue
  127. }
  128. }
  129. }