- 1、用分类封装系统的 UIAlertController,代码无侵入,简洁高效,一个block 搞定系统alert 和 actionSheet
- 2、可以自定义按钮数量、按钮颜色【多按钮样式,颜色数组和title数组个数不等情况下,默认蓝色】【待开发】
- 3、可以自定义 NSMutableAttributedString 的 alert 和 actionSheet,让你的 alert 更加炫酷【待开发】
- 4、完美适配 iPhone 和 iPad 版本
- 5、理论完全兼容现有 iOS 8 以上系统版本
如果发现 pod search BAAlertController-Swift
搜索出来的不是最新版本,需要在终端执行 cd 转换文件路径命令退回到 desktop,然后执行 pod setup
命令更新本地spec缓存(可能需要几分钟),然后再搜索就可以了。
具体步骤:
- pod setup : 初始化
- pod repo update : 更新仓库
- pod search BAAlertController-Swift
- 2、文件夹拖入:下载demo,把 BAAlertController-Swift 文件夹拖入项目即可,
- 3、导入头文件:
Swift import BAAlertController_Swift
- 4、项目源码地址:
OC 版 :https://github.com/BAHome/BAAlertController
Swift 版 :https://github.com/BAHome/BAAlertController-Swift
import Foundation
import UIKit
public typealias BAKit_UIAlertController_ButtonActionBlock = (_ index:Int, _ alertController:UIAlertController) -> ()
public typealias BAKit_UIAlertController_TextFieldConfigurationActionBlock = (_ textField:UITextField, _ index:Int) -> ()
extension UIAlertController {
/// UIAlertController:快速创建一个 普通的 alert
///
/// - Parameters:
/// - viewController: viewController
/// - title: 标题
/// - message: 内容
/// - buttonTitleArray: 按钮标题数组
/// - buttonTitleColorArray: 按钮标题颜色数组
/// - actionBlock: 按钮点击事件 block
public func ba_alertController(_ viewController: UIViewController,
title: String?,
attributedTitle: NSMutableAttributedString?,
message: String?,
attributedMessage: NSMutableAttributedString?,
buttonTitleArray:Array<String?>,
buttonTitleColorArray:Array<UIColor?>,
actionBlock : @escaping BAKit_UIAlertController_ButtonActionBlock) {
self.ba_creatAlertController(UIAlertController.Style.alert, viewController: viewController, title: title, attributedTitle:attributedTitle, message: message, attributedMessage:attributedMessage, buttonTitleArray: buttonTitleArray, buttonTitleColorArray: buttonTitleColorArray, buttonDisabledWithTitleArray: [], textFieldPlaceholderArray: [], textFieldConfigurationActionBlock: nil , actionBlock: actionBlock)
}
/// UIAlertController:快速创建一个 普通的 actionSheet
///
/// - Parameters:
/// - viewController: viewController
/// - title: 标题
/// - message: 内容
/// - buttonTitleArray: 按钮标题数组
/// - buttonTitleColorArray: 按钮标题颜色数组
/// - actionBlock: 按钮点击事件 block
public func ba_actionSheet(_ viewController: UIViewController,
title: String?,
attributedTitle: NSMutableAttributedString?,
message: String?,
attributedMessage: NSMutableAttributedString?,
buttonTitleArray:Array<String?>,
buttonTitleColorArray:Array<UIColor?>,
actionBlock : @escaping BAKit_UIAlertController_ButtonActionBlock) {
self.ba_creatAlertController(UIAlertController.Style.actionSheet, viewController: viewController, title: title, attributedTitle:attributedTitle, message: message, attributedMessage: attributedMessage, buttonTitleArray: buttonTitleArray, buttonTitleColorArray: buttonTitleColorArray, buttonDisabledWithTitleArray: [], textFieldPlaceholderArray: [], textFieldConfigurationActionBlock: nil , actionBlock: actionBlock)
}
/// UIAlertController: 快速创建一个系统 普通 带 textField 的 alert
///
/// - Parameters:
/// - viewController: viewController
/// - title: 标题
/// - message: 内容
/// - buttonTitleArray: 按钮标题数组
/// - buttonTitleColorArray: 按钮标题颜色数组
/// - buttonDisabledWithTitleArray: 按钮标题颜色数组(默认不能点击)
/// - textFieldPlaceholderArray: textField 的 Placeholder 数组
/// - textFieldConfigurationActionBlock: textField 配置 block
/// - actionBlock: 按钮点击事件 block
public func ba_alertController(_ viewController: UIViewController,
title: String?,
attributedTitle: NSMutableAttributedString?,
message: String?,
attributedMessage: NSMutableAttributedString?,
buttonTitleArray:Array<String?>,
buttonTitleColorArray:Array<UIColor?>,
buttonDisabledWithTitleArray:Array<String?>,
textFieldPlaceholderArray:Array<String?>,
textFieldConfigurationActionBlock: BAKit_UIAlertController_TextFieldConfigurationActionBlock?, actionBlock : @escaping BAKit_UIAlertController_ButtonActionBlock) {
self.ba_creatAlertController(UIAlertController.Style.alert, viewController: viewController, title: title, attributedTitle:attributedTitle, message: message, attributedMessage:attributedMessage, buttonTitleArray: buttonTitleArray, buttonTitleColorArray: buttonTitleColorArray, buttonDisabledWithTitleArray: buttonDisabledWithTitleArray, textFieldPlaceholderArray: textFieldPlaceholderArray, textFieldConfigurationActionBlock: textFieldConfigurationActionBlock, actionBlock: actionBlock)
}
private func ba_creatAlertController(_ preferredStyle:UIAlertController.Style,
viewController: UIViewController,
title: String?,
attributedTitle: NSMutableAttributedString?,
message: String?,
attributedMessage: NSMutableAttributedString?,
buttonTitleArray:Array<String?>,
buttonTitleColorArray:Array<UIColor?>,
buttonDisabledWithTitleArray:Array<String?>,
textFieldPlaceholderArray:Array<String?>,
textFieldConfigurationActionBlock: BAKit_UIAlertController_TextFieldConfigurationActionBlock?,
actionBlock : BAKit_UIAlertController_ButtonActionBlock?) {
// //消息内容样式(灰色斜体)
// let messageFontDescriptor = UIFontDescriptor.init(fontAttributes: [
// UIFontDescriptor.AttributeName.family:"Arial",
// UIFontDescriptor.AttributeName.name:"Arial-ItalicMT",
// ])
//
// let messageFont = UIFont.init(descriptor: messageFontDescriptor, size: 13.0)
// let messageAttribute = NSMutableAttributedString.init(string: message)
// messageAttribute.addAttributes([NSAttributedStringKey.font:messageFont,
// NSAttributedStringKey.foregroundColor:UIColor.red],
// range:NSMakeRange(0, (message.characters.count)))
let alertController = UIAlertController.init(title: title, message: message, preferredStyle: preferredStyle)
if ((attributedTitle?.length) != nil) {
alertController.setValue(attributedTitle, forKey: "attributedTitle")
}
if ((attributedMessage?.length) != nil) {
alertController.setValue(attributedMessage, forKey: "attributedMessage")
}
if buttonTitleArray.count > 0
{
for i:Int in 0..<buttonTitleArray.count
{
let buttonTitle = buttonTitleArray[i]
let action = UIAlertAction.init(title: buttonTitle, style: UIAlertAction.Style.default, handler: { (action) in
actionBlock!(i, alertController)
})
alertController.addAction(action)
for j:Int in 0..<buttonDisabledWithTitleArray.count
{
if (buttonDisabledWithTitleArray[j]?.elementsEqual(buttonTitle!))!
{
action.isEnabled = false
}
}
// 处理按钮字体颜色,待后期版本更新
var buttonTitleColorArray = buttonTitleColorArray
if buttonTitleColorArray.count == 0 || buttonTitleColorArray.count < buttonTitleArray.count {
var mutArr = [Any]()
for _:Int in 0..<buttonTitleArray.count {
mutArr.append(UIColor.blue)
}
buttonTitleColorArray = mutArr as! [UIColor]
}
if buttonTitleColorArray.count > 0 {
action.setValue(buttonTitleColorArray[i], forKey: "titleTextColor")
}
}
}
if preferredStyle == UIAlertController.Style.alert && textFieldPlaceholderArray.count > 0
{
for i:Int in 0..<textFieldPlaceholderArray.count
{
alertController.addTextField(configurationHandler: { (textField) in
textField.placeholder = textFieldPlaceholderArray[i]
textFieldConfigurationActionBlock!(textField, i)
})
}
}
if preferredStyle == UIAlertController.Style.actionSheet
{
let action = UIAlertAction.init(title: "取消", style: UIAlertAction.Style.cancel, handler: nil)
alertController.addAction(action)
}
viewController.ba_currentViewController()?.present(alertController, animated: true, completion: nil)
}
}
extension UIViewController {
public func ba_currentViewController() -> UIViewController? {
var presentedVC = UIApplication.shared.keyWindow?.rootViewController
while let pVC = presentedVC?.presentedViewController
{
presentedVC = pVC
}
if presentedVC == nil {
print("UIAlertController Error: You don't have any views set. You may be calling in viewDidload. Try viewDidappear.")
}
return presentedVC
}
}
func ba_alert() {
UIAlertController().ba_alertController(self, title: title0, attributedTitle:nil, message: msg0, attributedMessage:nil, buttonTitleArray: buttonTitleArray, buttonTitleColorArray: []) { (index, alertController) in
print("点我干嘛?index:", index)
}
}
func ba_alert2() -> Void {
let attributedTitle = NSMutableAttributedString.init(string: title0)
attributedTitle.addAttributes([NSAttributedString.Key.font:UIFont.boldSystemFont(ofSize: 30),
NSAttributedString.Key.foregroundColor:UIColor.purple],
range:NSMakeRange(0, (title0.count)))
//消息内容样式(灰色斜体)
let messageFontDescriptor = UIFontDescriptor.init(fontAttributes: [
UIFontDescriptor.AttributeName.family:"Arial",
UIFontDescriptor.AttributeName.name:"Arial-ItalicMT",
])
let messageFont = UIFont.init(descriptor: messageFontDescriptor, size: 13.0)
let attributedMessage = NSMutableAttributedString.init(string: msg0)
attributedMessage.addAttributes([NSAttributedString.Key.font:messageFont,
NSAttributedString.Key.foregroundColor:UIColor.red],
range:NSMakeRange(0, (msg0.count)))
UIAlertController().ba_actionSheet(self, title: nil, attributedTitle: attributedTitle, message: nil, attributedMessage: attributedMessage, buttonTitleArray: buttonTitleArray, buttonTitleColorArray: buttonTitleColorArray) { (index, alertController) in
print("点我干嘛?index:", index)
}
}
func ba_alert3() -> Void {
UIAlertController().ba_alertController(self, title: title0, attributedTitle:nil, message: msg0, attributedMessage:nil, buttonTitleArray: buttonTitleArray, buttonTitleColorArray: [], buttonDisabledWithTitleArray: ["确定"], textFieldPlaceholderArray: ["请输入账号", "请输入密码"], textFieldConfigurationActionBlock: { (textField, index) in
if index == 0
{
textField.keyboardType = .numberPad
}
if index == 1
{
textField.isSecureTextEntry = true
}
NotificationCenter.default.addObserver(self, selector: #selector(self.handleAlertTextFieldDidChangeAction(_ :)), name: NSNotification.Name.UITextFieldTextDidChange, object: textField)
}) { (index, alertController) in
}
}
@objc func handleAlertTextFieldDidChangeAction(_ notification: Notification) -> Void {
// 通知处理,判断文字输入的长度 大于 3 的时候,确定按钮可点击,否则,不可点击
let alertController:UIAlertController = self.presentedViewController as! UIAlertController;
let login:UITextField = alertController.textFields![0];
let sureAction:UIAlertAction = alertController.actions[1];
let loginString:String = login.text!
sureAction.isEnabled = loginString.count > 3;
}
其他示例可下载demo查看源码!
欢迎使用 【BAHome】 系列开源代码 !
如有更多需求,请前往:【BAHome】
项目源码地址:
OC 版 :https://github.com/BAHome/BAAlertController
Swift 版 :https://github.com/BAHome/BAAlertController-Swift
最新更新时间:2018-11-06 【倒叙】
最新Version:【Version:1.0.1】
更新内容:
1.0.1.1、UIAlertController-alert/actionSheet,可以自由设置按钮颜色、attributedTitle、attributedMessage
1.0.1.2、全新 pod 改版,支持 pod 1.0.1 版本, pod 后,需导入头文件:BAAlertController_Swift
1.0.1.3、部分代码优化,全面适配 swift 4.2
最新更新时间:2018-01-06 【倒叙】
最新Version:【Version:1.0.0】
更新内容:
1.0.0.1、普通的 UIAlertController-alert,可以自由设置按钮数量、按钮颜色
1.0.0.2、普通的 UIAlertController-alert,可以自由设置按钮数量、按钮颜色【多按钮样式,颜色数组和title数组个数不等】
1.0.0.3、普通的 带 textField 的 UIAlertController-alert,可添加一个或者多个 textField
1.0.0.4、普通的 UIAlertController-actionSheet,可以自由设置按钮数量、按钮颜色
1.0.0.5、完美适配 iPhone 和 iPad 版本
1.0.0.6、理论完全兼容现有 iOS 8 以上系统版本
1、开发中遇到 bug,希望小伙伴儿们能够及时反馈与我们 BAHome 团队,我们必定会认真对待每一个问题!
2、以后提需求和 bug 的同学,记得把 git 或者博客链接给我们,我直接超链到你们那里!希望大家积极参与测试!
1、QQ 群 479663605
【注意:此群为 2 元 付费群,介意的小伙伴儿勿扰!】
孙博岩
QQ:137361770
git:https://github.com/boai
简书:http://www.jianshu.com/u/95c9800fdf47
微博:
马景丽
QQ:1253540493
git:https://github.com/MaJingli
陆晓峰
QQ:442171865
git:https://github.com/zeR0Lu
陈集
QQ:3161182978
git:https://github.com/chenjipdc
简书:http://www.jianshu.com/u/90ae559fc21d
任子丰
QQ:459643690
git:https://github.com/renzifeng
吴丰收
QQ:498121294
石少庸
QQ:363605775
git:https://github.com/CrazyCoderShi
简书:http://www.jianshu.com/u/0726f4d689a3
开发使用 最新版本 Xcode,理论上支持 iOS 9 及以上版本,如有版本适配问题,请及时反馈!多谢合作!
感谢 【BAHome】 团队成员倾力合作,后期会推出一系列 常用 UI 控件的封装,大家有需求得也可以在 issue 提出,如果合理,我们会尽快推出新版本!
【BAHome】 的发展离不开小伙伴儿的信任与推广,再次感谢各位小伙伴儿的支持!