// // 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 = "ok".localized, 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: "yes".localized, style: .destructive, handler: handler)) alertController.addAction(UIAlertAction(title: "no".localized, 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: "ok".localized, style: .destructive, handler: handler)) alertController.addAction(UIAlertAction(title: "cancle".localized, 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".localized, 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: "tips".localized, message: errorDesc) } else if let retMsg = error.retMsg { self.showHintController(title: "tips".localized, message: retMsg) } else { self.showHintController(title: "tips".localized, message: "unknownErrorType".localized) } } else { self.showHintController(title: "tips".localized, message: "systembusy".localized) } } /// 当键盘弹起时解决输入框被遮挡问题 /// /// - 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) } } }