TYAlertController iOS 告警弹窗组件 API 参考:全部属性、生命周期回调与便捷分类方法完整清单
TYAlertController iOS 告警弹窗组件 API 参考:全部属性、生命周期回调与便捷分类方法完整清单
本文是 TYAlertController 的完整 API 参考。TYAlertController 是一款面向 iOS 的 Objective-C 告警弹窗/弹出层组件库,提供类 UIAlertController 的使用体验,支持自定义视图、模糊(毛玻璃)特效与自定义转场动画,兼容 iPhone 与 iPad。下文按"主控制器属性 → 内容视图属性 → 生命周期回调 → 便捷分类方法 → 动画与特效"的顺序,给出可直接查阅的接口清单。
核心 API 一览:先认识这 7 个类
在查细节之前,先建立整体地图。整个库的公开接口集中在这几个头文件中:
| 类型 | 类 / 分类 | 职责 |
|---|---|---|
| 主控制器 | TYAlertController |
弹窗控制器,负责展示、消失与转场动画,见 TYAlertController.h |
| 内容视图 | TYAlertView |
现成的标题 + 消息 + 按钮 + 输入框视图,见 TYAlertView.h |
| 按钮动作 | TYAlertAction |
弹窗按钮的模型(标题、样式、点击回调) |
| 便捷分类 | UIView (TYAlertView) |
一行代码在 controller 或 window 上弹出/隐藏视图,见 UIView+TYAlertView.h |
| 窗口容器 | TYShowAlertView |
不依赖控制器的 window 级弹窗容器 |
| 动画基类 | TYBaseAnimation |
自定义转场动画的基类,见 TYBaseAnimation.h |
| 毛玻璃分类 | TYAlertController (BlurEffects) |
给弹窗背后的视图加模糊特效,见 TYAlertController+BlurEffects.h |
💡 入门只需要两个头文件:
#import "UIView+TYAlertView.h"即可弹出视图;需要毛玻璃时再#import "TYAlertController+BlurEffects.h"(用法见 README.md)。
一、TYAlertController:主控制器全部属性
TYAlertController 继承自 UIViewController,通过工厂方法创建后用 presentViewController: 展示,用法与系统 UIAlertController 高度一致。
两个核心枚举:样式与转场动画
TYAlertControllerStyle(弹窗样式)
| 枚举值 | 说明 |
|---|---|
TYAlertControllerStyleAlert |
居中告警样式(默认) |
TYAlertControllerStyleActionSheet |
底部上滑的 ActionSheet 样式 |
TYAlertTransitionAnimation(内置转场动画)
| 枚举值 | 说明 |
|---|---|
TYAlertTransitionAnimationFade |
淡入淡出(默认) |
TYAlertTransitionAnimationScaleFade |
缩放 + 淡入 |
TYAlertTransitionAnimationDropDown |
从顶部落下 |
TYAlertTransitionAnimationCustom |
自定义动画类 |
常用可读写属性清单
| 属性 | 类型 | 说明 |
|---|---|---|
alertView |
UIView *(只读) |
当前承载的内容视图 |
backgroundColor |
UIColor * |
背景遮罩颜色 |
backgroundView |
UIView * |
可替换为任意自定义背景视图 |
preferredStyle |
枚举(只读) | 创建时指定的弹窗样式 |
transitionAnimation |
枚举(只读) | 当前使用的内置转场动画 |
transitionAnimationClass |
Class(只读) |
当前使用的自定义动画类 |
backgoundTapDismissEnable |
BOOL |
点击背景是否可关闭,默认 NO |
alertViewOriginY |
CGFloat |
弹窗顶部 Y 坐标,默认垂直居中 |
alertStyleEdging |
CGFloat |
Alert 样式左右边距,默认 15 |
actionSheetStyleEdging |
CGFloat |
ActionSheet 样式左右边距,默认 0 |
工厂方法与消失接口
+alertControllerWithAlertView:—— 最简创建,Alert 样式 + 默认动画+alertControllerWithAlertView:preferredStyle:—— 指定弹窗样式+alertControllerWithAlertView:preferredStyle:transitionAnimation:—— 再指定内置转场动画+alertControllerWithAlertView:preferredStyle:transitionAnimationClass:—— 指定自定义动画类(继承TYBaseAnimation)-dismissViewControllerAnimated:—— 主动关闭弹窗
生命周期回调:5 个 Block 覆盖弹窗全过程
这是该库区别于普通弹窗库的重要能力(定义见 TYAlertController.h):
| 回调 Block | 触发时机 |
|---|---|
viewWillShowHandler |
内容视图即将显示 |
viewDidShowHandler |
内容视图显示完成 |
viewWillHideHandler |
内容视图即将隐藏 |
viewDidHideHandler |
内容视图隐藏完成 |
dismissComplete |
控制器整体 dismiss 完成 |
TYAlertController *alertController = [TYAlertController alertControllerWithAlertView:alertView
preferredStyle:TYAlertControllerStyleAlert];
alertController.viewDidShowHandler = ^(UIView *alertView) { NSLog(@"已显示"); };
alertController.dismissComplete = ^{ NSLog(@"已关闭"); };
📌 实际使用示例可参考 ViewController.m,演示了完整挂接全部 5 个回调的写法。
二、TYAlertView 与 TYAlertAction:构建标准弹窗内容
TYAlertView 是内置的"标题 + 消息 + 输入框 + 按钮"视图,属性分四组,全部可在展示前调整(完整定义见 TYAlertView.h):
| 分组 | 属性 | 作用 |
|---|---|---|
| 文本 | titleLable、messageLabel |
标题 / 消息标签(只读访问) |
| 文本 | textFieldArray |
已添加的输入框数组 |
| 布局 | alertViewWidth(默认 280)、contentViewSpace、textLabelSpace、textLabelContentViewEdge |
控制整体宽度与留白 |
| 按钮 | buttonHeight、buttonSpace、buttonContentViewEdge、buttonContentViewTop、buttonCornerRadius、buttonFont |
按钮尺寸、间距、圆角、字体 |
| 按钮配色 | buttonDefaultBgColor、buttonCancelBgColor、buttonDestructiveBgColor |
三种按钮样式的底色 |
| 输入框 | textFieldBorderColor、textFieldBackgroudColor、textFieldFont、textFieldHeight、textFieldEdge、textFieldBorderWidth、textFieldContentViewEdge |
输入框外观 |
| 行为 | clickedAutoHide |
点击按钮后是否自动隐藏弹窗 |
内容添加 API:
+alertViewWithTitle:message:—— 创建带标题与消息的实例-addAction:—— 追加一个TYAlertAction按钮-addTextFieldWithConfigurationHandler:—— 追加输入框,Block 内可设置 placeholder 等
TYAlertAction 属性与方法:
| 成员 | 说明 |
|---|---|
+actionWithTitle:style:handler: |
创建按钮(标题、样式、点击 Block) |
title / style(只读) |
按钮标题与样式 |
enabled |
是否可点击 |
按钮样式枚举 TYAlertActionStyle 共三种:TYAlertActionStyleDefault、TYAlertActionStyleCancel、TYAlertActionStyleDestructive(建议给 Destructive 按钮配醒目的 buttonDestructiveBgColor)。
三、UIView+TYAlertView:最常用的一行代码便捷分类
日常开发中,你多半不需要手动创建 TYAlertController,直接用 UIView 分类的快捷方法即可(定义见 UIView+TYAlertView.h):
在 Controller 上展示
| 方法 | 说明 |
|---|---|
-showInController: |
Alert 样式展示 |
-showInController:preferredStyle: |
指定 Alert / ActionSheet 样式 |
-showInController:preferredStyle:backgoundTapDismissEnable: |
追加"点背景关闭"开关 |
-showInController:preferredStyle:transitionAnimation: |
指定内置转场动画 |
-showInController:preferredStyle:transitionAnimation:backgoundTapDismissEnable: |
样式 + 动画 + 点背景关闭,全参数版 |
在 Window 上展示(跨页面全局可用)
| 方法 | 说明 |
|---|---|
-showInWindow |
在 window 上展示 |
-showInWindowWithBackgoundTapDismissEnable: |
附带点背景关闭开关 |
-showInWindowWithOriginY: |
指定弹窗顶部 Y 坐标 |
-showInWindowWithOriginY:backgoundTapDismissEnable: |
Y 坐标 + 点背景关闭 |
隐藏与辅助方法
-hideView:自动判断当前来源并调用正确的隐藏方法-hideInController/-hideInWindow:分别针对两种展示方式+createViewFromNib/+createViewFromNibName::从同名(或指定名)xib 加载自定义视图,常用于弹出自定义视图,如 Demo 中的 ShareView.m-viewController:取视图所属的控制器
TYAlertView *alertView = [TYAlertView alertViewWithTitle:@"提示" message:@"确认执行该操作吗?"];
[alertView showInWindowWithOriginY:200 backgoundTapDismissEnable:YES];
四、TYShowAlertView:不依赖控制器的窗口级容器
如果你偏好"类方法直接弹"的 API,TYShowAlertView(见 TYShowAlertView.h)提供了与 window 直接交互的容器:
| 成员 | 说明 |
|---|---|
+showAlertViewWithView: |
直接展示任意视图 |
+showAlertViewWithView:backgoundTapDismissEnable: |
附加点背景关闭 |
+showAlertViewWithView:originY: / +showAlertViewWithView:originY:backgoundTapDismissEnable: |
指定 Y 坐标的版本 |
+alertViewWithView: |
创建容器实例(可持有以便后续控制) |
-show / -hide |
实例方式展示 / 隐藏 |
backgoundTapDismissEnable、alertViewOriginY、alertViewEdging |
与主控制器同名的行为属性 |
UIView+TYAlertView 的 showInWindow 系列本质就是封装了这一容器,两种写法可任选其一。
五、动画、布局与毛玻璃:进阶 API
自定义转场动画:继承 TYBaseAnimation
TYBaseAnimation 实现 UIViewControllerAnimatedTransitioning,继承后重写三个方法即可(见 TYBaseAnimation.h):
| 方法 | 作用 |
|---|---|
-transitionDuration: |
控制转场时长 |
-presentAnimateTransition: |
入场动画 |
-dismissAnimateTransition: |
出场动画 |
工厂方法 +alertAnimationIsPresenting: 与 +alertAnimationIsPresenting:preferredStyle: 用于区分入场/出场方向,后者可按样式返回 nil 表示不处理。内置的三套动画实现均很薄,是绝佳参考:TYAlertFadeAnimation.h、TYAlertScaleFadeAnimation.h、TYAlertDropDownAnimation.h。
TYAlertController 还带有 TYAlertController (TransitionAnimate) 分类,实现了 UIViewControllerTransitioningDelegate,负责把自定义动画类接入转场(见 TYAlertController+TransitionAnimate.m)。
UIView+TYAutoLayout:AutoLayout 快捷方法
自定义弹窗视图的约束编写可借助该分类(见 UIView+TYAutoLayout.h),常用方法包括 addConstraintToView:edgeInset:、addConstraintWithView:topView:leftView:bottomView:rightView:edgeInset:、addConstraintWithLeftView:toRightView:constant:、addConstraintWidth:height:、addConstraintCenterXToView:centerYToView: 以及 removeAllConstraints 等,覆盖"四边约束 / 相邻约束 / 定宽高 / 居中"等常见场景。
BlurEffects 毛玻璃分类
给弹窗背后的视图加模糊,只需一行(枚举见 TYAlertController+BlurEffects.h):
[alertController setBlurEffectWithView:self.view]; // 默认浅色
[alertController setBlurEffectWithView:self.view style:BlurEffectStyleDarkEffect]; // 深色
[alertController setBlurEffectWithView:self.view effectTintColor:[UIColor grayColor]]; // 自定义色调
BlurEffectStyle 共三种:BlurEffectStyleLight、BlurEffectStyleExtraLight、BlurEffectStyleDarkEffect。实现依赖 UIImage+ImageEffects.m,如需此功能请连 "Blur Effects" 目录一起拷入工程。
六、场景速查表:该用哪个 API?
| 你的需求 | 推荐 API |
|---|---|
| 标准"标题+按钮"弹窗 | TYAlertView + showInController:preferredStyle: |
| 需要输入框(账号/密码等) | addTextFieldWithConfigurationHandler: + textFieldArray 取值 |
| 全局浮层(不挂在某个控制器上) | showInWindowWithOriginY:backgoundTapDismissEnable: |
| 弹出 xib 中的自定义视图 | createViewFromNib + showInController: / showInWindow |
| 弹窗打开/关闭埋点、埋日志 | viewWillShowHandler 等 5 个生命周期回调 + dismissComplete |
| 弹窗背后毛玻璃 | setBlurEffectWithView:style: |
| 想要的动画库里没有 | 继承 TYBaseAnimation,走 transitionAnimationClass 工厂方法 |
| 点背景关闭 | backgoundTapDismissEnable(属性或同名参数) |
版本与环境要求
- 📦 安装:CocoaPods
pod 'TYAlertController',当前版本 1.2.0(见 TYAlertController.podspec);也可直接拷贝TYAlertControllerDemo/TYAlertController/目录,需要毛玻璃时再拷贝TYAlertControllerDemo/Blur Effects/目录 - 📱 环境:iOS 7.0+,Xcode 5+,要求开启 ARC
- 📄 更多示例:README.md 与演示工程 TYAlertControllerDemo/ViewController.m
