Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // Created by Michał Gorzałczany on 04.01.2018.
- // Copyright © 2018 Michał Gorzałczany. All rights reserved.
- //
- import UIKit
- enum MGAlertActionType {
- case Default
- case Destructive
- func textColor() -> UIColor {
- switch self {
- case .Default: return UIColor.darkBlue()
- case .Destructive: return UIColor.red
- }
- }
- }
- struct MGAlertAction {
- let title : String
- let type : MGAlertActionType
- let handler: (() -> Void)?
- let exit : Bool
- init(title: String? = "",
- type: MGAlertActionType = .Default,
- shouldExit : Bool = true,
- handler: (() -> Void)? = nil) {
- self.title = title!
- self.type = type
- self.handler = handler
- self.exit = shouldExit
- }
- }
- final class MGAlertViewController: UIViewController {
- private var alertView: UIView!
- private var blurView: UIView!
- private var titleLabel: UILabel!
- private var messageLabel: UILabel!
- private var bodyStackView: UIStackView!
- private var buttonsStackView: UIStackView!
- private var actions = [MGAlertAction]()
- private(set) var insideView : UIView?
- private(set) var alertTitle : String?
- private(set) var alertMessage : String?
- convenience init(title : String?, message : String?) {
- self.init()
- alertTitle = title
- alertMessage = message
- }
- func showAlert(fromViewController: UIViewController? = UIApplication.shared.keyWindow?.rootViewController) {
- modalPresentationStyle = .overCurrentContext
- modalTransitionStyle = .crossDissolve
- fromViewController?.present(self, animated: true, completion: nil)
- }
- override func viewDidLoad() {
- super.viewDidLoad()
- configureView()
- configureActions()
- configureInsideView()
- let selector = #selector(dismiss as () -> Void)
- let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: selector)
- tap.cancelsTouchesInView = false
- blurView.addGestureRecognizer(tap)
- }
- func addInsideView(_ insideView: UIView) {
- self.insideView = insideView
- }
- func dismiss(){
- self.dismiss(animated: true, completion: nil)
- }
- func dismiss(_ action: MGAlertAction) {
- self.dismiss(animated: true) {
- action.handler?()
- }
- }
- func configureInsideView() {
- guard let insideView = self.insideView else {return}
- bodyStackView.addArrangedSubview(insideView)
- }
- func addAction(_ action: MGAlertAction) {
- actions.append(action)
- }
- func configureActions() {
- for action in actions {
- let button = ClosureButton(type: .system)
- button.setTitle(action.title)
- button.backgroundColor = UIColor.white
- button.setTitleColor(action.type.textColor(), for: .normal)
- button.heightAnchor.constraint(equalToConstant: 45).isActive = true
- button.didTouchUpInside = { [weak self] _ in
- if action.exit {
- self?.dismiss(action)
- } else {
- action.handler?()
- }
- }
- buttonsStackView.addArrangedSubview(button)
- if buttonsStackView.arrangedSubviews.count > 3 {
- buttonsStackView.axis = .vertical
- }
- }
- }
- func configureView() {
- view.backgroundColor = UIColor.clear
- alertView = UIView()
- blurView = UIView()
- buttonsStackView = UIStackView()
- alertView.translatesAutoresizingMaskIntoConstraints = false
- blurView.translatesAutoresizingMaskIntoConstraints = false
- buttonsStackView.translatesAutoresizingMaskIntoConstraints = false
- //blur View
- view.addSubview(blurView)
- view.addConstraints([
- NSLayoutConstraint(item: blurView, attribute: .width, relatedBy: .equal, toItem: view, attribute: .width, multiplier: 1, constant: 0),
- NSLayoutConstraint(item: blurView, attribute: .height, relatedBy: .equal, toItem: view, attribute: .height, multiplier: 1, constant: 0),
- NSLayoutConstraint(item: blurView, attribute: .centerX, relatedBy: .equal, toItem: view, attribute: .centerX, multiplier: 1, constant: 0),
- NSLayoutConstraint(item: blurView, attribute: .centerY, relatedBy: .equal, toItem: view, attribute: .centerY, multiplier: 1, constant: 0)
- ])
- //Alert View
- alertView.backgroundColor = UIColor.white
- alertView.clipsToBounds = true
- view.addSubview(alertView)
- view.addConstraints([
- NSLayoutConstraint(item: alertView, attribute: .width, relatedBy: .equal, toItem: view, attribute: .width, multiplier: 0.8, constant: 0),
- NSLayoutConstraint(item: alertView, attribute: .height, relatedBy: .lessThanOrEqual, toItem: view, attribute: .height, multiplier: 0.8, constant: 0),
- NSLayoutConstraint(item: alertView, attribute: .centerX, relatedBy: .equal, toItem: view, attribute: .centerX, multiplier: 1, constant: 0),
- NSLayoutConstraint(item: alertView, attribute: .centerY, relatedBy: .equal, toItem: view, attribute: .centerY, multiplier: 1, constant: 0)
- ])
- //Body Stack View
- bodyStackView = UIStackView()
- bodyStackView.translatesAutoresizingMaskIntoConstraints = false
- bodyStackView.axis = .vertical
- alertView.addSubview(bodyStackView)
- view.addConstraints([
- NSLayoutConstraint(item: bodyStackView, attribute: .top, relatedBy: .equal, toItem: alertView, attribute: .top, multiplier: 1, constant: 16),
- NSLayoutConstraint(item: bodyStackView, attribute: .trailing, relatedBy: .equal, toItem: alertView, attribute: .trailing, multiplier: 1, constant: -16),
- NSLayoutConstraint(item: bodyStackView, attribute: .leading, relatedBy: .equal, toItem: alertView, attribute: .leading, multiplier: 1, constant: 16),
- ])
- //Title Label
- if let title = alertTitle {
- titleLabel = UILabel()
- titleLabel.numberOfLines = 0
- titleLabel.textAlignment = .center
- titleLabel.text = title
- bodyStackView.addArrangedSubview(titleLabel)
- }
- //Message Label
- if let message = alertMessage {
- messageLabel = UILabel()
- messageLabel.numberOfLines = 0
- messageLabel.textAlignment = .center
- messageLabel.text = message
- bodyStackView.addArrangedSubview(messageLabel)
- }
- //Buttons Stack View
- alertView.addSubview(buttonsStackView)
- view.addConstraints([
- NSLayoutConstraint(item: buttonsStackView, attribute: .top, relatedBy: .equal, toItem: bodyStackView, attribute: .bottom, multiplier: 1, constant: 16),
- NSLayoutConstraint(item: buttonsStackView, attribute: .bottom, relatedBy: .equal, toItem: alertView, attribute: .bottom, multiplier: 1, constant: 0),
- NSLayoutConstraint(item: buttonsStackView, attribute: .trailing, relatedBy: .equal, toItem: alertView, attribute: .trailing, multiplier: 1, constant: 0),
- NSLayoutConstraint(item: buttonsStackView, attribute: .leading, relatedBy: .equal, toItem: alertView, attribute: .leading, multiplier: 1, constant: 0),
- ])
- //stack view spacer
- let spacerView = UIView()
- spacerView.backgroundColor = UIColor.lightGray
- spacerView.translatesAutoresizingMaskIntoConstraints = false
- alertView.insertSubview(spacerView, belowSubview: buttonsStackView)
- view.addConstraints([
- NSLayoutConstraint(item: spacerView, attribute: .width, relatedBy: .equal, toItem: buttonsStackView, attribute: .width, multiplier: 1, constant: 0),
- NSLayoutConstraint(item: spacerView, attribute: .height, relatedBy: .equal, toItem: buttonsStackView, attribute: .height, multiplier: 1, constant: 0.5),
- NSLayoutConstraint(item: spacerView, attribute: .centerX, relatedBy: .equal, toItem: buttonsStackView, attribute: .centerX, multiplier: 1, constant: 0),
- NSLayoutConstraint(item: spacerView, attribute: .centerY, relatedBy: .equal, toItem: buttonsStackView, attribute: .centerY, multiplier: 1, constant: -0.5)
- ])
- //Settings
- alertView.layer.cornerRadius = 0
- blurView.backgroundColor = UIColor.darkGray
- blurView.alpha = 0.5
- buttonsStackView.spacing = 0.5
- spacerView.alpha = 0.2
- buttonsStackView.layoutMargins = UIEdgeInsetsMake(0, 8, 0, 8)
- buttonsStackView.axis = .horizontal
- buttonsStackView.distribution = .fillEqually
- }
- }
- class ClosureButton: UIButton {
- typealias DidTapButton = (ClosureButton) -> ()
- var didTouchUpInside: DidTapButton? {
- didSet {
- if didTouchUpInside != nil {
- addTarget(self, action: #selector(ClosureButton.didTouchUpInside(sender:)), for: .touchUpInside)
- } else {
- removeTarget(self, action: #selector(ClosureButton.didTouchUpInside(sender:)), for: .touchUpInside)
- }
- }
- }
- func didTouchUpInside(sender: UIButton) {
- if let handler = didTouchUpInside {
- handler(self)
- }
- }
- }
Add Comment
Please, Sign In to add comment