Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // KMAlertViewController.swift
- //
- // Created by Kazi Abdullah Al Mamun on 13/2/20.
- // Copyright © 2020 Kazi Abdullah Al Mamun. All rights reserved.
- //
- import UIKit
- class KMAlertViewController: UIViewController {
- private var titleMsg: String?
- private var message: String?
- private var okAction: ((KMAlertViewController) -> Void)?
- private var cancelAction: ((KMAlertViewController) -> Void)?
- private let containerView: UIView = {
- let view = UIView()
- view.backgroundColor = #colorLiteral(red: 0.9607843137, green: 0.9607843137, blue: 0.9607843137, alpha: 1)
- view.setCornerRadius(5)
- return view
- }()
- private lazy var titleLabel: UILabel = {
- let label = UILabel()
- label.textColor = #colorLiteral(red: 0.4588235294, green: 0.4588235294, blue: 0.4588235294, alpha: 1)
- label.font = UIFont.systemFont(ofSize: 24)
- label.text = self.titleMsg ?? ""
- label.numberOfLines = 1
- label.textAlignment = .center
- return label
- }()
- private lazy var messageLabel: UILabel = {
- let label = UILabel()
- label.textColor = #colorLiteral(red: 0.4745098039, green: 0.4745098039, blue: 0.4745098039, alpha: 1)
- label.font = UIFont.systemFont(ofSize: 18)
- label.text = self.message ?? ""
- label.numberOfLines = 0
- label.textAlignment = .center
- return label
- }()
- private lazy var okBtn: UIButton = {
- let btn = UIButton()
- btn.setTitle("Ok", for: .normal)
- btn.setTitleColor(.white, for: .normal)
- btn.backgroundColor = #colorLiteral(red: 0.2470588235, green: 0.662745098, blue: 0.9607843137, alpha: 1)
- btn.setCornerRadius(5)
- btn.addTarget(self, action: #selector(okBtnAction(_:)), for: .touchUpInside)
- return btn
- }()
- private let cancelBtn: UIButton = {
- let btn = UIButton()
- btn.setTitle("Cancel", for: .normal)
- btn.setTitleColor(#colorLiteral(red: 0.6784313725, green: 0.6784313725, blue: 0.6784313725, alpha: 1), for: .normal)
- btn.backgroundColor = #colorLiteral(red: 0.9411764706, green: 0.9411764706, blue: 0.9411764706, alpha: 1)
- btn.setCornerRadius(5)
- btn.addTarget(self, action: #selector(cancelBtnAction(_:)), for: .touchUpInside)
- return btn
- }()
- override func loadView() {
- view = UIView()
- view.backgroundColor = #colorLiteral(red: 0, green: 0, blue: 0, alpha: 0.5135916096)
- view.addSubview(containerView)
- containerView.translatesAutoresizingMaskIntoConstraints = false
- NSLayoutConstraint.activate([
- containerView.centerYAnchor.constraint(equalTo: view.centerYAnchor),
- containerView.centerXAnchor.constraint(equalTo: view.centerXAnchor),
- containerView.widthAnchor.constraint(equalTo: view.widthAnchor, multiplier: 0.8)
- ])
- containerView.addSubview(titleLabel)
- titleLabel.translatesAutoresizingMaskIntoConstraints = false
- NSLayoutConstraint.activate([
- titleLabel.topAnchor.constraint(equalTo: containerView.topAnchor, constant: 16),
- titleLabel.leadingAnchor.constraint(equalTo: containerView.leadingAnchor, constant: 32),
- titleLabel.trailingAnchor.constraint(equalTo: containerView.trailingAnchor, constant: -32)
- ])
- containerView.addSubview(messageLabel)
- messageLabel.translatesAutoresizingMaskIntoConstraints = false
- NSLayoutConstraint.activate([
- messageLabel.topAnchor.constraint(equalTo: titleLabel.bottomAnchor, constant: 12),
- messageLabel.centerXAnchor.constraint(equalTo: titleLabel.centerXAnchor),
- messageLabel.widthAnchor.constraint(equalTo: titleLabel.widthAnchor, multiplier: 1)
- ])
- let btnStackView = UIStackView()
- btnStackView.axis = .horizontal
- btnStackView.spacing = 16
- btnStackView.distribution = .fillEqually
- btnStackView.addArrangedSubview(cancelBtn)
- btnStackView.addArrangedSubview(okBtn)
- containerView.addSubview(btnStackView)
- btnStackView.translatesAutoresizingMaskIntoConstraints = false
- NSLayoutConstraint.activate([
- btnStackView.heightAnchor.constraint(equalToConstant: 50),
- btnStackView.topAnchor.constraint(equalTo: messageLabel.bottomAnchor, constant: 40),
- btnStackView.leadingAnchor.constraint(equalTo: containerView.leadingAnchor, constant: 16),
- btnStackView.trailingAnchor.constraint(equalTo: containerView.trailingAnchor, constant: -16),
- btnStackView.bottomAnchor.constraint(equalTo: containerView.bottomAnchor, constant: -16)
- ])
- }
- init(title: String?, message: String?) {
- self.titleMsg = title
- self.message = message
- super.init(nibName: nil, bundle: nil)
- self.modalPresentationStyle = .overFullScreen
- }
- required init?(coder: NSCoder) {
- fatalError("init(coder:) has not been implemented")
- }
- override func viewDidLoad() {
- super.viewDidLoad()
- // Do any additional setup after loading the view.
- }
- //Mark: Member Functions
- public func addAction(for event: ActionEvent, action: @escaping (KMAlertViewController) -> Void) {
- switch event {
- case .Ok:
- self.okAction = action
- case .Cancel:
- self.cancelAction = action
- }
- }
- //Mark: Actions
- @objc func okBtnAction(_ sendr: UIButton) {
- guard let okAction = self.okAction else {
- self.dismiss(animated: false)
- return
- }
- okAction(self)
- }
- @objc func cancelBtnAction(_ sendr: UIButton) {
- guard let action = self.cancelAction else {
- self.dismiss(animated: false)
- return
- }
- action(self)
- }
- }
- extension KMAlertViewController {
- public enum ActionEvent {
- case Ok
- case Cancel
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement