123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188 |
- import UIKit
- public enum FFAlertStyle {
- case actionSheet
- case alert
- }
- public class FFAlertController: UIViewController {
- private var transitionDelegate: FFTransitioning
- private var alert: UIView & FFAlertRepresentable
- public var alertStyle: FFAlertStyle
- public var contentEdgeInsets: UIEdgeInsets {
- get { return self.alert.contentEdgInsets }
- set { alert.contentEdgInsets = newValue }
- }
- public var attributedTitle: NSAttributedString? {
- get { return self.alert.title }
- set { self.alert.title = newValue }
- }
- public var attributedMessage: NSAttributedString? {
- get { return self.alert.message }
- set { self.alert.message = newValue }
- }
- public override var title: String? {
- get { return attributedTitle?.string }
- 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)]) }
- }
- public var message: String? {
- get { return attributedMessage?.string }
- 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)]) }
- }
- private var alertActions: [FFAlertAction] {
- get { return alert.alertActions }
- set { alert.alertActions = newValue }
- }
- public var alertEdgInsets: (left: CGFloat, right: CGFloat) {
- get { return alert.edgInsets }
- set { alert.edgInsets = newValue }
- }
- convenience init(title: String? = nil, message: String? = nil,
- customView: UIView,
- alertStyle: FFAlertStyle = .alert) {
- self.init(alertStyle: alertStyle)
- self.alert.contentView = customView
- self.title = title
- self.message = message
- }
- convenience init(title: String? = nil, message: String? = nil,
- alertStyle: FFAlertStyle = .alert) {
- self.init(alertStyle: alertStyle)
- self.title = title
- self.message = message
- }
- convenience init(attributedTitle: NSAttributedString? = nil,
- attributedMessage: NSAttributedString? = nil,
- alertStyle: FFAlertStyle = .alert) {
- self.init(alertStyle: alertStyle)
- self.attributedTitle = attributedTitle
- self.attributedMessage = attributedMessage
- }
- private init(alertStyle: FFAlertStyle) {
- switch alertStyle {
- case .alert:
- self.alert = FFAlertView()
- case .actionSheet:
- self.alert = FFSheetView()
- }
- self.alertStyle = alertStyle
- self.transitionDelegate = FFTransitioning(alertStyle: alertStyle)
- super.init(nibName: nil, bundle: nil)
- commonInit()
- }
- required public init?(coder aDecoder: NSCoder) {
- fatalError("init(coder:) has not been implemented")
- }
- private func commonInit() {
- self.modalPresentationStyle = .custom
- self.transitioningDelegate = self.transitionDelegate
- }
- override public var preferredStatusBarStyle: UIStatusBarStyle {
- return presentingViewController?.preferredStatusBarStyle ?? .default
- }
- override public var supportedInterfaceOrientations: UIInterfaceOrientationMask {
- return presentingViewController?.supportedInterfaceOrientations ?? super.supportedInterfaceOrientations
- }
- override public func viewDidLoad() {
- super.viewDidLoad()
- configurationOfAlertView()
- let tap = UITapGestureRecognizer(target: self, action: #selector(disappear))
- tap.delegate = self
- view.addGestureRecognizer(tap)
- }
- @objc
- func disappear() {
- dismiss(animated: true, completion: nil)
- }
- func configurationOfAlertView() {
- view.addSubview(alert)
- alert.translatesAutoresizingMaskIntoConstraints = false
- alert.backgroundColor = UIColor.white
- switch alertStyle {
- case .actionSheet:
- NSLayoutConstraint.activate(
- [NSLayoutConstraint(item: alert, attribute: .leading,
- relatedBy: .equal,
- toItem: view, attribute: .leading,
- multiplier: 1, constant: alertEdgInsets.left),
- NSLayoutConstraint(item: alert, attribute: .bottom,
- relatedBy: .equal,
- toItem: view, attribute: .bottom,
- multiplier: 1, constant: 0),
- NSLayoutConstraint(item: alert, attribute: .trailing,
- relatedBy: .equal,
- toItem: view, attribute: .trailing,
- multiplier: 1, constant: -alertEdgInsets.right),
- NSLayoutConstraint(item: alert, attribute: .height,
- relatedBy: .lessThanOrEqual,
- toItem: view, attribute: .height,
- multiplier: 1, constant: 0)]
- )
- alert.setContentCompressionResistancePriority(.required, for: .vertical)
- case .alert:
- NSLayoutConstraint.activate(
- [NSLayoutConstraint(item: alert, attribute: .leading,
- relatedBy: .equal,
- toItem: view, attribute: .leading,
- multiplier: 1, constant: alertEdgInsets.left),
- NSLayoutConstraint(item: alert, attribute: .centerY,
- relatedBy: .equal,
- toItem: view, attribute: .centerY,
- multiplier: 1, constant: 0),
- NSLayoutConstraint(item: alert, attribute: .trailing,
- relatedBy: .equal,
- toItem: view, attribute: .trailing,
- multiplier: 1, constant: -alertEdgInsets.right),
- NSLayoutConstraint(item: alert, attribute: .height,
- relatedBy: .lessThanOrEqual,
- toItem: view, attribute: .height,
- multiplier: 1, constant: 0)]
- )
- alert.setContentCompressionResistancePriority(.required, for: .vertical)
- }
- }
- func addAlertAction(alertAction: FFAlertAction) {
- alertActions.append(alertAction)
- }
- }
- extension FFAlertController: UIGestureRecognizerDelegate {
- public func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldReceive touch: UITouch) -> Bool {
- if alert.frame.contains(touch.location(in: view)) {
- return false
- }
- return true
- }
- }
|