| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301 |
- //
- // UIViewController+Alert.swift
- // MTP2_iOS
- //
- // Created by zhongyuan on 2018/5/10.
- // Copyright © 2018年 zhongyuan.All rights reserved.
- //
- import UIKit
- extension UIViewController {
- // MARK: - 提示信息框
- /// 显示提示信息框的方法(单个确定按钮)
- ///
- /// - Parameters:
- /// - title: 标题
- /// - message: 提示内容
- /// - handler: 确定按钮点击后的回调块
- func showHintController(title: String, message: String, comfirmTitle: String = "确定", handler: ((UIAlertAction) -> Swift.Void)? = nil) {
- let alertController = UIAlertController(title: title, message: message, preferredStyle: .alert)
- alertController.addAction(UIAlertAction(title: comfirmTitle, style: .cancel, handler: handler))
- alertController.view.tintColor = UIColor.darkText
- self.present(alertController, animated: true, completion: nil)
- }
-
- /// 显示警告信息框的方法(自动隐藏)
- ///
- /// - Parameters:
- /// - title: 标题
- /// - message: 提示内容
- /// - completion: 自动隐藏后的回调块
- func showWarnController(title: String, message: String, completion: (() -> Swift.Void)? = nil) {
- let alertController = UIAlertController(title: title, message: message, preferredStyle: UIAlertController.Style.alert)
- self.present(alertController, animated: true, completion: { () -> Void in
- let delayTime = DispatchTime.now() + Double(Int64(1 * Double(NSEC_PER_SEC))) / Double(NSEC_PER_SEC)
- DispatchQueue.main.asyncAfter(deadline: delayTime, execute: { () -> Void in
- alertController.dismiss(animated: true, completion: completion)
- })
- })
- }
-
- /// 显示提示确定信息框
- ///
- /// - Parameters:
- /// - title: 标题
- /// - message: 提示内容
- /// - handler: 确定信息后的回调块
- func showYesOrNOController(title: String, message: String, handler: ((UIAlertAction) -> Swift.Void)? = nil) {
- let alertController = UIAlertController(title: title, message: message, preferredStyle: .alert)
- alertController.addAction(UIAlertAction(title: "确定", style: .destructive, handler: handler))
- alertController.addAction(UIAlertAction(title: "取消", style: .cancel, handler: nil))
- alertController.view.tintColor = UIColor.darkText
- self.present(alertController, animated: true, completion: nil)
- }
-
- /// 显示提示确定信息框
- ///
- /// - Parameters:
- /// - title: 标题
- /// - message: 提示内容
- /// - handler: 确定信息后的回调块
- func showCommitAlertController(title: String, message: String, handler: ((UIAlertAction) -> Swift.Void)? = nil) {
- let alertController = UIAlertController(title: title, message: message, preferredStyle: .alert)
- alertController.addAction(UIAlertAction(title: "确定", style: .destructive, handler: handler))
- alertController.addAction(UIAlertAction(title: "取消", style: .cancel, handler: nil))
- alertController.view.tintColor = UIColor.darkText
- self.present(alertController, animated: true, completion: nil)
- }
-
- /// 显示 ActionSheetController 弹出框信息
- ///
- /// - Parameters:
- /// - title: 标题
- /// - message: 内容信息
- /// - cancelTitle: 取消按钮标题
- /// - cancelBlock: 取消执行块
- /// - actionSheetTitles: actionSheetTitles 标题
- /// - handler: actionSheet 执行块
- func showActionSheetController(title : String?, message : String?, cancelTitle : String?, cancelBlock: (() -> Void)?, actionSheetTitles: [String]?, handler: ((_ index: NSInteger, _ action: UIAlertAction?) -> Void)?) {
- /// show AlertController
- let alertController = UIAlertController.init(title: title, message: message, preferredStyle: .actionSheet)
-
- /// cancel Action
- if let title = cancelTitle {
- let cancelAction = UIAlertAction.init(title: title, style: .cancel, handler: { (action) in
- if cancelBlock != nil {
- cancelBlock!()
- }
- })
- alertController.addAction(cancelAction)
- }
-
- alertController.view.tintColor = UIColor.darkText
-
- /// ActionSheet
- if let titles = actionSheetTitles {
- /// 构建sheets
- for sheetTitle in titles {
- let actionSheetAction = UIAlertAction.init(title: sheetTitle, style: .default, handler: { (alertAction) in
- let index = titles.firstIndex(of: sheetTitle)
- if handler != nil {
- handler!(index!, alertAction)
- }
- })
- alertController.addAction(actionSheetAction)
- }
- }
-
- /// modal present alertController
- self.present(alertController, animated: true, completion: nil)
- }
-
- /// 显示 AlertController 弹出框信息
- ///
- /// - Parameters:
- /// - title: 标题
- /// - message: 内容信息
- /// - cancelTitle: 取消按钮标题
- /// - sureTitle: 确认按钮标题
- /// - cancelBlock: 取消执行块
- /// - sureBlock: 确认执行块
- func showAlertController(title: String?, message: String?, cancelTitle: String?, sureTitle: String?, cancelBlock:(() -> Void)?, sureBlock:(() -> Void)?) {
-
- /// show AlertController
- let alertController = UIAlertController.init(title: title, message: message, preferredStyle: .alert)
-
- /// cancel Action
- if cancelTitle != nil {
- let cancelAction = UIAlertAction.init(title: cancelTitle, style: .cancel, handler: { (action) in
- if cancelBlock != nil {
- cancelBlock!()
- }
- })
- alertController.addAction(cancelAction)
- }
- alertController.view.tintColor = UIColor.darkText
-
- /// sure Action
- if sureTitle != nil {
- let sureAction = UIAlertAction.init(title: sureTitle, style: .destructive, handler: { (action) in
- if sureBlock != nil {
- sureBlock!()
- }
- })
- alertController.addAction(sureAction)
- }
-
- /// modal present alertController
- self.present(alertController, animated: true, completion: nil)
- }
-
- /// 展示给定数量信息的弹出选择框
- ///
- /// - Parameters:
- /// - title: 弹出框标题
- /// - message: 弹出框提示信息
- /// - takeInfo: 携带信息
- /// - type: 弹出类型
- /// - clickBlock: 代码块
- func showPopupsController(title: String?, message: String?, titleArray: [String], takeInfo: Any? = nil,type: UIAlertController.Style = .alert, _ showCancel: Bool = true, clickBlock: @escaping ((_ value: String,_ rowNum: Int, _ takeInfo: Any?) -> Void)) {
-
- /// show AlertController
- let alertController = UIAlertController(title: title, message: message, preferredStyle: type)
- alertController.view.tintColor = UIColor.darkText
-
- if showCancel == true {
- /// cancel Action
- let cancelAction = UIAlertAction(title: "cancle", style: .cancel, handler: nil)
- alertController.addAction(cancelAction)
- }
-
- /// Actions
- for (index, title) in titleArray.enumerated() {
- let action = UIAlertAction(title: title, style: .default, handler: { (_) in
- clickBlock(title,index, takeInfo)
- })
- alertController.addAction(action)
- }
-
- let height: NSLayoutConstraint = NSLayoutConstraint(item: alertController.view!, attribute: NSLayoutConstraint.Attribute.height, relatedBy: NSLayoutConstraint.Relation.lessThanOrEqual, toItem: nil, attribute: NSLayoutConstraint.Attribute.notAnAttribute, multiplier: 1, constant: self.view.frame.height * 0.50)
- alertController.view.addConstraint(height)
-
- /// modal present alertController
- self.present(alertController, animated: true, completion: nil)
- }
-
- /// 根据错误码展示提示信息
- ///
- /// - Parameter error: ErrorInfo
- func showErrorMessgae(error: ErrorInfo?) {
- if let error = error {
- if let errorDesc = ErrorUtils.getDesc(errorCode: error.retCode) {
- self.showHintController(title: "提示", message: errorDesc)
- } else if let retMsg = error.retMsg {
- self.showHintController(title: "提示", message: retMsg)
- } else {
- self.showHintController(title: "提示", message: "未知错误")
- }
- } else {
- self.showHintController(title: "提示", message: "系统繁忙")
- }
- }
-
- /// 当键盘弹起时解决输入框被遮挡问题
- ///
- /// - Parameter views: 需要使用软键盘输入的文本控件列表
- func moveView(views: [UIView]) {
- NotificationCenter.default.addObserver(forName: UIResponder.keyboardWillChangeFrameNotification, object: nil, queue: nil) { [weak self] (notification) in
- guard let weakSelf = self, let responseView = views.first(where: { $0.isFirstResponder }) else { return }
-
- // 获取键盘的坐标
- let endFrame = (notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as! NSValue).cgRectValue
- let keyboardY: CGFloat = kScreenHeight - endFrame.origin.y
- // 获取目标的Y值
- let targetY: CGFloat = responseView.convert(CGPoint(x: 0, y: responseView.height), to: weakSelf.view).y
- // 为防止遮挡需要偏移的距离
- let offset = targetY + keyboardY - weakSelf.view.height + 24 // 24是textfield和键盘上方的间距,可以自由设定
-
- UIView.animate(withDuration: 0.25) {
- if offset > 0 {
- weakSelf.view.bounds = CGRect(x: 0, y: offset, width: weakSelf.view.width, height: weakSelf.view.height)
- } else {
- weakSelf.view.bounds = CGRect(x: 0, y: 0, width: weakSelf.view.width, height: weakSelf.view.height)
- }
- }
- }
- }
-
- /// 找到以本viewController为基础中最顶层的viewController,若没有模态视图,则最顶层的viewController就是它本身
- var presentedViewControllerVisibleInWindow: UIViewController {
- var vc = self
- while (vc.presentedViewController != nil) {
- vc = vc.presentedViewController!
- }
- return vc
- }
- }
- extension UIViewController {
- /// viewController
- /// - Parameters:
- /// - name: name
- /// - identifier: identifier
- /// - Returns: UIViewController
- func viewController (name: String, identifier: String) -> UIViewController {
- return UIStoryboard(name: name, bundle: nil).instantiateViewController(withIdentifier: identifier)
- }
- }
- extension UIViewController {
- /// push
- /// - Parameters:
- /// - sbname: storyboardName
- /// - sbId: storyboardId
- /// - takeInfo: takeInfo
- /// - checkLogin: checkLogin
- func push(storyboardName sbname:String,
- storyboardId sbId:String,
- takeInfo: Any? = nil,
- checkLogin: Bool = true) {
- if !checkLogin || MTP2BusinessCore.shared.getLoginStatus() { /// 检查登录状态
- let board: UIStoryboard = UIStoryboard.init(name: sbname, bundle: nil)
- if let baseViewController = board.instantiateViewController(withIdentifier: sbId) as? BaseViewController,
- let model = takeInfo {
- baseViewController.takeInfo = model
- self.navigationController?.pushViewController(baseViewController, animated: true)
- } else {
- let viewController = board.instantiateViewController(withIdentifier: sbId)
- self.navigationController?.pushViewController(viewController, animated: true)
- }
- } else { /// 未登录
- let login = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "Login")
- self.navigationController?.pushViewController(login, animated: true)
- }
- }
-
- /// present
- /// - Parameters:
- /// - sbname: sbname
- /// - sbId: sbId
- /// - isNav: isNav
- /// - style: modalTransitionStyle
- func present(storyboardName sbname:String,
- storyboardId sbId:String,
- needNavigationController isNav: Bool = true,
- modalTransitionStyle style: UIModalTransitionStyle = .crossDissolve) {
- let board: UIStoryboard = UIStoryboard.init(name: sbname, bundle: nil)
- let viewController = board.instantiateViewController(withIdentifier: sbId)
-
- if isNav == true {
- let navigationController = BaseNavigationController(rootViewController: viewController)
- navigationController.modalPresentationStyle = .overFullScreen
- navigationController.modalTransitionStyle = style
- self.present(navigationController, animated: true, completion: nil)
- } else {
- viewController.modalPresentationStyle = .overFullScreen
- viewController.modalTransitionStyle = style
- self.present(viewController, animated: true, completion: nil)
- }
- }
- }
|