You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

147 lines
5.3 KiB

//
// UIViewExtension.swift
// GMERemittance
//
// Created by Fm-user on 12/23/17.
// Copyright © 2017 Gobal Money Express Co. Ltd. All rights reserved.
//
import Foundation
import UIKit
import MBProgressHUD
extension UIView {
func roundCorners(_ corners: UIRectCorner, radius: CGFloat, borderColor: UIColor?, borderWidth: CGFloat?) {
let path = UIBezierPath(roundedRect: self.bounds, byRoundingCorners: corners, cornerRadii: CGSize(width: radius, height: radius))
let mask = CAShapeLayer()
mask.frame = self.bounds
mask.path = path.cgPath
self.layer.mask = mask
if borderWidth != nil {
addBorder(mask, borderWidth: borderWidth!, borderColor: borderColor!)
}
}
private func addBorder(_ mask: CAShapeLayer, borderWidth: CGFloat, borderColor: UIColor) {
let borderLayer = CAShapeLayer()
borderLayer.path = mask.path
borderLayer.fillColor = UIColor.clear.cgColor
borderLayer.strokeColor = borderColor.cgColor
borderLayer.lineWidth = borderWidth
borderLayer.frame = bounds
layer.addSublayer(borderLayer)
}
}
// ccr
// MARK: Alerts
extension UIViewController {
func confirmationAlert(title: String, message: String, confirmTitle: String, style: UIAlertActionStyle = .destructive, confirmAction: @escaping () -> Void) {
let deleteActionSheetController = UIAlertController(title: title, message: message, preferredStyle: .alert)
let deleteAction = UIAlertAction(title: confirmTitle, style: style) {
action -> Void in
confirmAction()
}
let cancelAction = UIAlertAction(title: "Cancel", style: .cancel) { action -> Void in
}
deleteActionSheetController.addAction(deleteAction)
deleteActionSheetController.addAction(cancelAction)
self.present(deleteActionSheetController, animated: true, completion: nil)
}
func alert(message: String?, title: String? = "Error", okAction: (()->())? = nil) {
let alertController = getAlert(message: message, title: title)
alertController.addAction(title: "Ok", handler: okAction)
self.present(alertController, animated: true, completion: nil)
}
func alertWithOkCancel(message: String?, title: String? = "Error", okTitle: String? = "Ok", style: UIAlertControllerStyle? = .alert, cancelTitle: String? = "Cancel", OkStyle: UIAlertActionStyle = .default, cancelStyle: UIAlertActionStyle = .default, okAction: (()->())? = nil, cancelAction: (()->())? = nil) {
let alertController = getAlert(message: message, title: title, style: style)
alertController.addAction(title: okTitle,style: OkStyle, handler: okAction)
alertController.addAction(title: cancelTitle, style: cancelStyle, handler: cancelAction)
self.present(alertController, animated: true, completion: nil)
}
private func getAlert(message: String?, title: String?, style: UIAlertControllerStyle? = .alert) -> UIAlertController {
return UIAlertController(title: title, message: message, preferredStyle: style ?? .alert)
}
func present(_ alert: UIAlertController, asActionsheetInSourceView sourceView: Any) {
if UI_USER_INTERFACE_IDIOM() == .pad {
alert.modalPresentationStyle = .popover
if let presenter = alert.popoverPresentationController {
if sourceView is UIBarButtonItem {
presenter.barButtonItem = sourceView as? UIBarButtonItem
}else if sourceView is UIView {
let view = sourceView as! UIView
presenter.sourceView = view
presenter.sourceRect = view.bounds
}
}
}
self.present(alert, animated: true, completion: nil)
}
}
extension UIAlertController {
func addAction(title: String?, style: UIAlertActionStyle = .default, handler: (()->())? = nil) {
let action = UIAlertAction(title: title, style: style, handler: {_ in
handler?()
})
self.addAction(action)
}
}
struct Associate {
static var hud: UInt8 = 0
}
// MARK: HUD
extension UIViewController {
private func setProgressHud() -> MBProgressHUD {
let progressHud: MBProgressHUD = MBProgressHUD.showAdded(to: self.view, animated: true)
progressHud.tintColor = UIColor.darkGray
progressHud.removeFromSuperViewOnHide = true
objc_setAssociatedObject(self, &Associate.hud, progressHud, objc_AssociationPolicy.OBJC_ASSOCIATION_RETAIN_NONATOMIC)
return progressHud
}
var progressHud: MBProgressHUD {
if let progressHud = objc_getAssociatedObject(self, &Associate.hud) as? MBProgressHUD {
progressHud.isUserInteractionEnabled = true
return progressHud
}
return setProgressHud()
}
var progressHudIsShowing: Bool {
return self.progressHud.isHidden
}
func showProgressHud() {
self.progressHud.show(animated: false)
}
func hideProgressHud() {
self.progressHud.label.text = ""
self.progressHud.completionBlock = {
objc_setAssociatedObject(self, &Associate.hud, nil, objc_AssociationPolicy.OBJC_ASSOCIATION_RETAIN_NONATOMIC)
}
self.progressHud.hide(animated: false)
}
}