HYAlertController是一款极简形式的Alert控件,包含多种使用场景,并且拥有和Apple的UIAlertController一样的语法,所以您可以轻松地在您自己的app中使用它。
Alert Style
Sheet Style
Share Style
特性
- 标题
- 介绍信息(自适应高度)
- 按钮可以带icon显示
- 自带取消按钮
- 新增分享风格
- 点击事件采用闭包语法回调
- 与UIAlertController相同的语法实现
- 支持Swift 3
- Cocoapods
- Carthage(暂不支持)
要求
- Swift 3
- iOS 10.0+
- Xcode 8+
CocoaPods
CocoaPods是iOS最常用的依赖管理工具,您可以用下面的命令安装它:
$ gem install cocoapods然后在项目根目录创建Podfile文件,写入下面内容:
source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '10.0'
use_frameworks!
pod 'HYAlertController'最后,命令行运行下面命令即可完成安装:
$ pod install注意:
HYAlertController会依赖安装SnapKit库,HYAlertController所有的自动布局都采用SnapKit完成,如果您的项目中也使用到了SnapKit,请避免依赖重复。
手动安装
- 下载该项目文件,将
/HYAlertController文件夹拖到您的项目中去; - 如果您的项目中没有使用
SnapKit,您还需要安装SnapKit,版本是3.0.0+;
注意:推荐您使用Cocoapods方式安装,这样可以避免繁琐的依赖问题,但是如果您是手动安装的话,一定要保证您的
SnapKit版本高于3.0.0。
用法
用法类似于UIAlertController,不过HYAlertController提供了三种风格: Alert、Sheet和Share。
Alert Style: 拥有这种风格,您可以居中显示内容,作为提醒用户操作所用的对话框;
Sheet Style: 拥有这种风格,您可以在屏幕下方显示内容,和微信、微博等的风格类似,下面会弹出一个对话框,供用户选择;
Share Style: 与Sheet Style类似,也是下面弹出一个对话框,所不同的是,这种样式可用于分享所用,您可以快速地完成主流分享样式的创建。
Alert Style
//Work with Swift 3
let alertVC: HYAlertController = HYAlertController (title: "Title", message: "Here you can describe the details of its title, and you can write here what you want to express.", style: .alert)
let oneAction: HYAlertAction = HYAlertAction (title: "One Action", style: .normal, handler: { (action) in
print(action.title)
})
let twoAction: HYAlertAction = HYAlertAction (title: "Two Action", style: .normal, handler: { (action) in
print(action.title)
})
let threeAction: HYAlertAction = HYAlertAction (title: "Three Action", style: .destructive, handler: { (action) in
print(action.title)
})
let cancelAction: HYAlertAction = HYAlertAction (title: "Cancel Action", style: .cancel, handler: { (action) in
print(action.title)
})
alertVC.addAction(action: oneAction)
alertVC.addAction(action: twoAction)
alertVC.addAction(action: threeAction)
alertVC.addAction(action: cancelAction)
self.present(alertVC, animated: true, completion: nil)
Sheet Style
//Work with Swift 3
let alertVC: HYAlertController = HYAlertController (title: "Title", message: "Here you can describe the details of its title, and you can write here what you want to express.", style: .actionSheet)
let oneAction: HYAlertAction = HYAlertAction (title: "One Action", style: .normal, handler: { (action) in
print(action.title)
})
let twoAction: HYAlertAction = HYAlertAction (title: "Two Action", style: .normal, handler: { (action) in
print(action.title)
})
let threeAction: HYAlertAction = HYAlertAction (title: "Three Action", style: .destructive, handler: { (action) in
print(action.title)
})
let cancelAction: HYAlertAction = HYAlertAction (title: "Cancel Action", style: .cancel, handler: { (action) in
print(action.title)
})
alertVC.addAction(action: oneAction)
alertVC.addAction(action: twoAction)
alertVC.addAction(action: threeAction)
alertVC.addAction(action: cancelAction)
self.present(alertVC, animated: true, completion: nil)
Share Style
//Work with Swift 3
let alertVC: HYAlertController = HYAlertController (title: nil, message: nil, style: .shareSheet)
let oneAction: HYAlertAction = HYAlertAction (title: "Facebook", image: UIImage (named: "facebook")!, style: .normal, handler: {
(action) in
print(action.title)
})
let twoAction: HYAlertAction = HYAlertAction (title: "Twitter", image: UIImage (named: "twitter")!, style: .normal, handler: {
(action) in
print(action.title)
})
let threeAction: HYAlertAction = HYAlertAction (title: "Snapchat", image: UIImage (named: "snapchat")!, style: .normal, handler: {
(action) in
print(action.title)
})
let fourAction: HYAlertAction = HYAlertAction (title: "Instagram", image: UIImage (named: "instagram")!, style: .normal, handler: {
(action) in
print(action.title)
})
let fiveAction: HYAlertAction = HYAlertAction (title: "Pinterest", image: UIImage (named: "pinterest")!, style: .normal, handler: {
(action) in
print(action.title)
})
let sixAction: HYAlertAction = HYAlertAction (title: "Line", image: UIImage (named: "line")!, style: .normal, handler: {
(action) in
print(action.title)
})
alertVC.addShareActions(actions: [oneAction, twoAction, threeAction, fourAction, fiveAction, sixAction])
self.present(alertVC, animated: true, completion: nil)
查看更多使用场景,请参考
HYAlertControllerDemo里详细介绍。
Swift版本要求
HYAlertController采用Swift 3开发完成,所以您的Swift版本必须是Swift 3。
自定义
HYAlertController本身外部并没有提供自定义选择,这和开发者的想法有关,如果您想做一些基本的改变,请下载项目源码,修改HY_Constants.swift文件,这里包含了一些基本的设置常量,修改这里即可完成自定义。
修改完成之后,可以参照上述手动安装方法将改造后的类库集成到项目中。
交流
- 如果您遇到问题或者是需要帮助,可以创建issue,我会第一时间为您解答;
- 如果您需要一些优化,可以创建issue讨论;
- 如果您想提交贡献,请发布一个pull request.
MIT License
HYAlertController is available under the MIT license. See the LICENSE file for more info.




