No Description

FFAlertController.swift 7.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. //
  2. // FFAlertController.swift
  3. // FFAlert
  4. //
  5. // Created by FFIB on 2017/11/13.
  6. // Copyright © 2017年 FFIB. All rights reserved.
  7. //
  8. import UIKit
  9. public enum FFAlertStyle {
  10. case actionSheet
  11. case alert
  12. }
  13. public class FFAlertController: UIViewController {
  14. // private
  15. private var transitionDelegate: FFTransitioning
  16. private var alert: UIView & FFAlertRepresentable
  17. public var alertStyle: FFAlertStyle
  18. public var contentEdgeInsets: UIEdgeInsets {
  19. get { return self.alert.contentEdgInsets }
  20. set { alert.contentEdgInsets = newValue }
  21. }
  22. public var attributedTitle: NSAttributedString? {
  23. get { return self.alert.title }
  24. set { self.alert.title = newValue }
  25. }
  26. public var attributedMessage: NSAttributedString? {
  27. get { return self.alert.message }
  28. set { self.alert.message = newValue }
  29. }
  30. public override var title: String? {
  31. get { return attributedTitle?.string }
  32. set { attributedTitle = NSAttributedString(string: newValue ?? "", attributes: [NSAttributedString.Key.font: UIFont.systemFont(ofSize: 17), NSAttributedString.Key.foregroundColor: UIColor.init(red: 0.2, green: 0.2, blue: 0.2, alpha: 1.0)]) }
  33. }
  34. public var message: String? {
  35. get { return attributedMessage?.string }
  36. set { attributedMessage = NSAttributedString(string: newValue ?? "", attributes: [NSAttributedString.Key.font: UIFont.systemFont(ofSize: 14), NSAttributedString.Key.foregroundColor: UIColor.init(red: 0.6, green: 0.6, blue: 0.6, alpha: 1.0)]) }
  37. }
  38. private var alertActions: [FFAlertAction] {
  39. get { return alert.alertActions }
  40. set { alert.alertActions = newValue }
  41. }
  42. public var alertEdgInsets: (left: CGFloat, right: CGFloat) {
  43. get { return alert.edgInsets }
  44. set { alert.edgInsets = newValue }
  45. }
  46. convenience init(title: String? = nil, message: String? = nil,
  47. customView: UIView,
  48. alertStyle: FFAlertStyle = .alert) {
  49. self.init(alertStyle: alertStyle)
  50. self.alert.contentView = customView
  51. self.title = title
  52. self.message = message
  53. }
  54. convenience init(title: String? = nil, message: String? = nil,
  55. alertStyle: FFAlertStyle = .alert) {
  56. self.init(alertStyle: alertStyle)
  57. self.title = title
  58. self.message = message
  59. }
  60. convenience init(attributedTitle: NSAttributedString? = nil,
  61. attributedMessage: NSAttributedString? = nil,
  62. alertStyle: FFAlertStyle = .alert) {
  63. self.init(alertStyle: alertStyle)
  64. self.attributedTitle = attributedTitle
  65. self.attributedMessage = attributedMessage
  66. }
  67. private init(alertStyle: FFAlertStyle) {
  68. switch alertStyle {
  69. case .alert:
  70. self.alert = FFAlertView()
  71. case .actionSheet:
  72. self.alert = FFSheetView()
  73. }
  74. self.alertStyle = alertStyle
  75. self.transitionDelegate = FFTransitioning(alertStyle: alertStyle)
  76. super.init(nibName: nil, bundle: nil)
  77. commonInit()
  78. }
  79. required public init?(coder aDecoder: NSCoder) {
  80. fatalError("init(coder:) has not been implemented")
  81. }
  82. private func commonInit() {
  83. self.modalPresentationStyle = .custom
  84. self.transitioningDelegate = self.transitionDelegate
  85. }
  86. override public var preferredStatusBarStyle: UIStatusBarStyle {
  87. return presentingViewController?.preferredStatusBarStyle ?? .default
  88. }
  89. override public var supportedInterfaceOrientations: UIInterfaceOrientationMask {
  90. return presentingViewController?.supportedInterfaceOrientations ?? super.supportedInterfaceOrientations
  91. }
  92. override public func viewDidLoad() {
  93. super.viewDidLoad()
  94. configurationOfAlertView()
  95. let tap = UITapGestureRecognizer(target: self, action: #selector(disappear))
  96. tap.delegate = self
  97. view.addGestureRecognizer(tap)
  98. }
  99. @objc
  100. func disappear() {
  101. dismiss(animated: true, completion: nil)
  102. }
  103. func configurationOfAlertView() {
  104. view.addSubview(alert)
  105. alert.translatesAutoresizingMaskIntoConstraints = false
  106. alert.backgroundColor = UIColor.white
  107. switch alertStyle {
  108. case .actionSheet:
  109. NSLayoutConstraint.activate(
  110. [NSLayoutConstraint(item: alert, attribute: .leading,
  111. relatedBy: .equal,
  112. toItem: view, attribute: .leading,
  113. multiplier: 1, constant: alertEdgInsets.left),
  114. NSLayoutConstraint(item: alert, attribute: .bottom,
  115. relatedBy: .equal,
  116. toItem: view, attribute: .bottom,
  117. multiplier: 1, constant: 0),
  118. NSLayoutConstraint(item: alert, attribute: .trailing,
  119. relatedBy: .equal,
  120. toItem: view, attribute: .trailing,
  121. multiplier: 1, constant: -alertEdgInsets.right),
  122. NSLayoutConstraint(item: alert, attribute: .height,
  123. relatedBy: .lessThanOrEqual,
  124. toItem: view, attribute: .height,
  125. multiplier: 1, constant: 0)]
  126. )
  127. alert.setContentCompressionResistancePriority(.required, for: .vertical)
  128. case .alert:
  129. NSLayoutConstraint.activate(
  130. [NSLayoutConstraint(item: alert, attribute: .leading,
  131. relatedBy: .equal,
  132. toItem: view, attribute: .leading,
  133. multiplier: 1, constant: alertEdgInsets.left),
  134. NSLayoutConstraint(item: alert, attribute: .centerY,
  135. relatedBy: .equal,
  136. toItem: view, attribute: .centerY,
  137. multiplier: 1, constant: 0),
  138. NSLayoutConstraint(item: alert, attribute: .trailing,
  139. relatedBy: .equal,
  140. toItem: view, attribute: .trailing,
  141. multiplier: 1, constant: -alertEdgInsets.right),
  142. NSLayoutConstraint(item: alert, attribute: .height,
  143. relatedBy: .lessThanOrEqual,
  144. toItem: view, attribute: .height,
  145. multiplier: 1, constant: 0)]
  146. )
  147. alert.setContentCompressionResistancePriority(.required, for: .vertical)
  148. }
  149. }
  150. func addAlertAction(alertAction: FFAlertAction) {
  151. alertActions.append(alertAction)
  152. }
  153. }
  154. extension FFAlertController: UIGestureRecognizerDelegate {
  155. public func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldReceive touch: UITouch) -> Bool {
  156. if alert.frame.contains(touch.location(in: view)) {
  157. return false
  158. }
  159. return true
  160. }
  161. }