Coder Social home page Coder Social logo

huangxianhui001 / adalertcontroller Goto Github PK

View Code? Open in Web Editor NEW
6.0 1.0 1.0 36.35 MB

与 UIAlertController 类似风格的 UI 控件,包含 Alert 和 ActionSheet 以及无边距的 ActionSheet 类型.支持添加自定义视图元素,多种AlertAction可供选择,并容易扩展实现新的 AlertAction UI样式,支持优先级队列

License: MIT License

Objective-C 99.28% C 0.15% Ruby 0.57%
uialertcontroller alertview actionsheet alertaction priorityqueue contentview ios objective-c

adalertcontroller's Introduction

说明

ADAlertController 是一个与 UIAlertController 类似风格的 UI 控件,包含 AlertActionSheet 以及无边距的 ActionSheet(ADAlertControllerStyleSheet) 等 UI类型.与UIAlertController 有相似的 API.支持 iOS9+.并支持以下扩展功能:

  • 支持添加自定义视图元素
  • 多种AlertAction可供选择,并容易扩展实现新的 AlertAction UI样式
  • 支持优先级队列,在优先级队列里可配置以下功能
    • 支持仅在某些特定的控制器上显示
    • 支持设置黑名单

演示

Demo 1

Demo 2

Demo 3

Demo 4

Demo 5

Demo 6

安装

  1. 使用CocoaPods:

    Podfile中添加以下语句

    pod ‘ADAlertController’

  2. 手动安装:

    下载仓库代码,将ADAlertController文件夹下所有的文件拖到你的项目工程中

    PS:ADCustomAlertContentView文件夹并非ADAlertController要求的源码,而是实现自定义内容视图的一点建议.若有其他更好的用法,欢迎提供意见.

若在使用过程中有任何问题,欢迎提Issue

使用

针对演示图片中的第一张图出现的第一个例子为例,初始化代码如下:

		ADAlertAction *cancelAction = [ADAlertAction actionWithTitle:@"取消" style:ADActionStyleCancel handler:^(__kindof ADAlertAction * _Nonnull action) {
        NSLog(@"点击了取消");
    }];
    
    ADAlertAction *sureAction = [ADAlertAction actionWithTitle:@"确定" style:ADActionStyleDefault handler:^(__kindof ADAlertAction * _Nonnull action) {
        NSLog(@"点击了确定");
    }];
    
    ADAlertController *alertView = [[ADAlertController alloc] initWithOptions:nil title:@"这里是标题" message:nil actions:@[cancelAction,sureAction]];
    [alertView show];
    

1.初始化

ADAlertController使用指定构造方法来生成实例:-initWithOptions:title:message:actions:.

  • 第一个参数是ADAlertControllerConfiguration对象类型,可以为 nil.

    ADAlertControllerConfiguration用来配置一些 UI 样式.其中的preferredStyle属性就是来指定具体的 UI 样式,有以下几个枚举类型:

    • ADAlertControllerStyleAlert:与UIAlertControllerStyleAlert类似
    • ADAlertControllerStyleActionSheet:与UIAlertControllerStyleActionSheet类似
    • ADAlertControllerStyleSheet:与UIAlertControllerStyleActionSheet类似,只是左右以及底部的边距都是 0.

    preferredStyle属性一经初始化,不得更改.

    ADAlertControllerConfiguration使用指定构造方法-initWithPreferredStyle:

  • 第二,第三个参数分别表示标题和内容信息,可为 nil.

  • 第三个参数是包含的按钮,可为 nil.不同于UIAlertController的添加按钮方法,这里添加按钮采用数组的方式.

    对于ADAlertControllerStyleAlert类型,这个数组的内容就是警告框最终包含的按钮.

    对于ADAlertControllerStyleActionSheetADAlertControllerStyleSheet类型,若想添加取消按钮,不是将取消按钮放到最后一个,而是应该调用-addActionSheetCancelAction:来添加.

2.按钮的初始化

ADAlertAction是具体的按钮类,用来生成标题或者图片的按钮,标题或图片只能二选一显示,若同时配置二者,则优先显示标题.构造方法为+ actionWithTitle:image:style:handler:configuration:

其中handler为点击事件回调.configuration为 UI 配置相关.

若需要实现自己的按钮 UI 样式,可以参考ADAlertImageAction的实现.

另外还提供了两个ADAlertAction的子类:

  • ADAlertGroupAction:可将多个ADAlertAction作为一个按钮添加,内部的各个按钮平分宽度
  • ADScrollableGroupAction:ADAlertGroupAction的子类,当按钮个数多个时,可滑动显示

3.显示与隐藏

显示时可直接调用-show方法.想主动隐藏可以调用-hiden方法.若想触摸背景也自动隐藏,可以设置ADAlertControllerConfigurationhidenWhenTapBackground属性为YES.

若想进入优先级队列,可以调用-enqueue

4.设置自定义视图

设置ADAertControllercontentView为你需要的UIView对象即可,需要自己设置高度约束.建议的方法为[contentView.heightAnchor constraintEqualToConstant:100].active = YES.

ADAlertControllerStyleAlert类型时,是显示在标题之上.

ADAlertControllerStyleActionSheetADAlertControllerStyleSheet类型时,显示在 message 之下

具体用法可参考示例中的ADAdvertViewADPickerView

*5.alert类型添加 UITextField

当为ADAlertControllerStyleAlert类型时可以添加textField.具体参考示例中的”添加输入框的警告框”

目前添加UITextField 暂未适配键盘遮挡问题

6.优先级及优先级队列

若需要显示多个AlertController又难以管理各个AlertController的显示顺序时,可以设置优先级属性alertPriority并调用入队列方法-enqueue.在内部有个优先级队列管理对象(ADAlertControllerPriorityQueue)会自动管理下一个将要显示的AlertController对象

使用优先级队列有以下注意点:

  1. alertPriorityNSUInteger类型,虽只有三个枚举值,但可以设置任何有效的NSUInteger值.
  2. 支持设置全局的黑名单.黑名单仅支持类型,不支持具体到某个控制器实例,具体参考示例中的"黑名单时不显示”
  3. 支持设置AlertController仅针对某个目标控制器显示.具体参考示例中的"指定在某个控制器显示”

adalertcontroller's People

Contributors

huangxianhui001 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Forkers

liuchongfaye

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.