Advertisement
nativecoder

CustomAlertController

Mar 20th, 2020
804
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 6.11 KB | None | 0 0
  1. //
  2. //  KMAlertViewController.swift
  3. //
  4. //  Created by Kazi Abdullah Al Mamun on 13/2/20.
  5. //  Copyright © 2020 Kazi Abdullah Al Mamun. All rights reserved.
  6. //
  7. import UIKit
  8.  
  9. class KMAlertViewController: UIViewController {
  10.    
  11.     private var titleMsg: String?
  12.     private var message: String?
  13.     private var okAction: ((KMAlertViewController) -> Void)?
  14.     private var cancelAction: ((KMAlertViewController) -> Void)?
  15.    
  16.     private let containerView: UIView = {
  17.         let view = UIView()
  18.         view.backgroundColor = #colorLiteral(red: 0.9607843137, green: 0.9607843137, blue: 0.9607843137, alpha: 1)
  19.         view.setCornerRadius(5)
  20.         return view
  21.     }()
  22.    
  23.     private lazy var titleLabel: UILabel = {
  24.         let label = UILabel()
  25.         label.textColor = #colorLiteral(red: 0.4588235294, green: 0.4588235294, blue: 0.4588235294, alpha: 1)
  26.         label.font = UIFont.systemFont(ofSize: 24)
  27.         label.text = self.titleMsg ?? ""
  28.         label.numberOfLines = 1
  29.         label.textAlignment = .center
  30.         return label
  31.     }()
  32.    
  33.     private lazy var messageLabel: UILabel = {
  34.         let label = UILabel()
  35.         label.textColor = #colorLiteral(red: 0.4745098039, green: 0.4745098039, blue: 0.4745098039, alpha: 1)
  36.         label.font = UIFont.systemFont(ofSize: 18)
  37.         label.text = self.message ?? ""
  38.         label.numberOfLines = 0
  39.         label.textAlignment = .center
  40.         return label
  41.     }()
  42.    
  43.     private lazy var okBtn: UIButton = {
  44.         let btn = UIButton()
  45.         btn.setTitle("Ok", for: .normal)
  46.         btn.setTitleColor(.white, for: .normal)
  47.         btn.backgroundColor = #colorLiteral(red: 0.2470588235, green: 0.662745098, blue: 0.9607843137, alpha: 1)
  48.         btn.setCornerRadius(5)
  49.         btn.addTarget(self, action: #selector(okBtnAction(_:)), for: .touchUpInside)
  50.         return btn
  51.     }()
  52.    
  53.     private let cancelBtn: UIButton = {
  54.         let btn = UIButton()
  55.         btn.setTitle("Cancel", for: .normal)
  56.         btn.setTitleColor(#colorLiteral(red: 0.6784313725, green: 0.6784313725, blue: 0.6784313725, alpha: 1), for: .normal)
  57.         btn.backgroundColor = #colorLiteral(red: 0.9411764706, green: 0.9411764706, blue: 0.9411764706, alpha: 1)
  58.         btn.setCornerRadius(5)
  59.         btn.addTarget(self, action: #selector(cancelBtnAction(_:)), for: .touchUpInside)
  60.         return btn
  61.     }()
  62.    
  63.     override func loadView() {
  64.        
  65.         view = UIView()
  66.         view.backgroundColor = #colorLiteral(red: 0, green: 0, blue: 0, alpha: 0.5135916096)
  67.        
  68.         view.addSubview(containerView)
  69.         containerView.translatesAutoresizingMaskIntoConstraints = false
  70.         NSLayoutConstraint.activate([
  71.             containerView.centerYAnchor.constraint(equalTo: view.centerYAnchor),
  72.             containerView.centerXAnchor.constraint(equalTo: view.centerXAnchor),
  73.             containerView.widthAnchor.constraint(equalTo: view.widthAnchor, multiplier: 0.8)
  74.         ])
  75.        
  76.         containerView.addSubview(titleLabel)
  77.         titleLabel.translatesAutoresizingMaskIntoConstraints = false
  78.         NSLayoutConstraint.activate([
  79.             titleLabel.topAnchor.constraint(equalTo: containerView.topAnchor, constant: 16),
  80.             titleLabel.leadingAnchor.constraint(equalTo: containerView.leadingAnchor, constant: 32),
  81.             titleLabel.trailingAnchor.constraint(equalTo: containerView.trailingAnchor, constant: -32)
  82.         ])
  83.        
  84.         containerView.addSubview(messageLabel)
  85.         messageLabel.translatesAutoresizingMaskIntoConstraints = false
  86.         NSLayoutConstraint.activate([
  87.             messageLabel.topAnchor.constraint(equalTo: titleLabel.bottomAnchor, constant: 12),
  88.             messageLabel.centerXAnchor.constraint(equalTo: titleLabel.centerXAnchor),
  89.             messageLabel.widthAnchor.constraint(equalTo: titleLabel.widthAnchor, multiplier: 1)
  90.         ])
  91.        
  92.         let btnStackView = UIStackView()
  93.         btnStackView.axis = .horizontal
  94.         btnStackView.spacing = 16
  95.         btnStackView.distribution = .fillEqually
  96.         btnStackView.addArrangedSubview(cancelBtn)
  97.         btnStackView.addArrangedSubview(okBtn)
  98.         containerView.addSubview(btnStackView)
  99.         btnStackView.translatesAutoresizingMaskIntoConstraints = false
  100.         NSLayoutConstraint.activate([
  101.             btnStackView.heightAnchor.constraint(equalToConstant: 50),
  102.             btnStackView.topAnchor.constraint(equalTo: messageLabel.bottomAnchor, constant: 40),
  103.             btnStackView.leadingAnchor.constraint(equalTo: containerView.leadingAnchor, constant: 16),
  104.             btnStackView.trailingAnchor.constraint(equalTo: containerView.trailingAnchor, constant: -16),
  105.             btnStackView.bottomAnchor.constraint(equalTo: containerView.bottomAnchor, constant: -16)
  106.         ])
  107.        
  108.     }
  109.    
  110.     init(title: String?, message: String?) {
  111.         self.titleMsg = title
  112.         self.message = message
  113.         super.init(nibName: nil, bundle: nil)
  114.         self.modalPresentationStyle = .overFullScreen
  115.     }
  116.    
  117.     required init?(coder: NSCoder) {
  118.         fatalError("init(coder:) has not been implemented")
  119.     }
  120.    
  121.     override func viewDidLoad() {
  122.         super.viewDidLoad()
  123.  
  124.         // Do any additional setup after loading the view.
  125.     }
  126.    
  127.     //Mark: Member Functions
  128.    
  129.     public func addAction(for event: ActionEvent, action: @escaping (KMAlertViewController) -> Void) {
  130.         switch event {
  131.         case .Ok:
  132.             self.okAction = action
  133.         case .Cancel:
  134.             self.cancelAction = action
  135.         }
  136.     }
  137.    
  138.     //Mark: Actions
  139.    
  140.     @objc func okBtnAction(_ sendr: UIButton) {
  141.         guard let okAction = self.okAction else {
  142.             self.dismiss(animated: false)
  143.             return
  144.         }
  145.         okAction(self)
  146.     }
  147.    
  148.     @objc func cancelBtnAction(_ sendr: UIButton) {
  149.         guard let action = self.cancelAction else {
  150.             self.dismiss(animated: false)
  151.             return
  152.         }
  153.         action(self)
  154.     }
  155. }
  156.  
  157. extension KMAlertViewController {
  158.     public enum ActionEvent {
  159.         case Ok
  160.         case Cancel
  161.     }
  162. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement