HYAlertController是一款极简形式的Alert控件,包含多种使用场景,并且拥有和Apple的UIAlertController
一样的语法,所以您可以轻松地在您自己的app中使用它。
- 标题
- 介绍信息(自适应高度)
- 按钮可以带icon显示
- 自带取消按钮
- 新增分享风格
- 点击事件采用闭包语法回调
- 与UIAlertController相同的语法实现
- 支持Swift 3
- Cocoapods
- Carthage
- Swift 3
- iOS 10.0+
- Xcode 8+
CocoaPods是iOS最常用的依赖管理工具,您可以用下面的命令安装它:
$ gem install cocoapods
然后在项目根目录创建Podfile
文件,写入下面内容:
source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '10.0'
use_frameworks!
pod 'HYAlertController'
最后,命令行运行下面命令即可完成安装:
$ pod install
下载该项目文件,将/HYAlertController
文件夹拖到您的项目中去。
用法类似于UIAlertController
,不过HYAlertController
提供了三种风格: Alert、Sheet和Share。
Alert Style: 拥有这种风格,您可以居中显示内容,作为提醒用户操作所用的对话框;
Sheet Style: 拥有这种风格,您可以在屏幕下方显示内容,和微信、微博等的风格类似,下面会弹出一个对话框,供用户选择;
Share Style: 与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: .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)
//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)
//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
里详细介绍。
HYAlertController
采用Swift 3开发完成,所以您的Swift版本必须是Swift 3。
HYAlertController
本身外部并没有提供自定义选择,这和开发者的想法有关,如果您想做一些基本的改变,请下载项目源码,修改HY_Constants.swift
文件,这里包含了一些基本的设置常量,修改这里即可完成自定义。
修改完成之后,可以参照上述手动安装方法将改造后的类库集成到项目中。
- 如果您遇到问题或者是需要帮助,可以创建issue,我会第一时间为您解答;
- 如果您需要一些优化,可以创建issue讨论;
- 如果您想提交贡献,请发布一个pull request.
HYAlertController is available under the MIT license. See the LICENSE file for more info.