TYAlertController Window层弹出详解:TYShowAlertView与UIView分类API速查指南
TYAlertController Window层弹出详解:TYShowAlertView与UIView分类API速查指南
TYAlertController 是一款强大的 iOS 弹窗库,支持在 controller 和 Window 层显示弹窗,内置模糊效果、自定义视图和自定义动画,兼容 iPhone 与 iPad。本文将聚焦它的 Window 层弹出机制:详解 TYShowAlertView 的核心属性与类方法,并给出 UIView+TYAlertView 分类 API 速查表,帮助你快速实现"任意位置弹出、全局置顶"的提示框。
两种弹出层级:Controller 层 vs Window 层
TYAlertController 提供了两种显示弹窗的方式,选对层级是第一步:
| 方式 | 入口 | 特点 | 适用场景 |
|---|---|---|---|
| Controller 层 | TYAlertController |
完整支持 Alert / ActionSheet 样式、4 种过渡动画、生命周期回调 | 常规业务弹窗 |
| Window 层 | TYShowAlertView |
直接挂到 keyWindow,全局置顶,切换页面不受影响 | Toast 提示、全局通知、跨页面浮层 |
Window 层弹出的核心思路很简单:把弹窗视图直接 addSubview 到 <a href="https://link.gitcode.com/i/64a2faf741950d11c048ab6873f66e0b" target="_blank">UIApplication sharedApplication].keyWindow 上(见 [TYShowAlertView.m#L165-L177)。这样无论当前显示的是哪个页面,弹窗都能浮在所有内容之上。
TYShowAlertView 是什么:Window 层弹出的核心类
TYShowAlertView 本质上是一个透明容器视图,它自动完成三件事:
- 黑色半透明遮罩:内部自动创建 40% 透明度的背景蒙层,并支持替换为自定义
backgroundView - 自动布局:弹窗默认水平居中,宽度未设置时按屏幕宽度减去左右各 15pt 边距(
alertViewEdging可调) - 缩放动画:显示/隐藏时带 0.3 秒的缩放 + 淡入淡出动画
它的常用属性与类方法如下(完整定义见 TYShowAlertView.h):
| 属性/方法 | 说明 | 默认值 |
|---|---|---|
alertViewOriginY |
弹窗距顶部的 Y 坐标,>0 时覆盖默认垂直居中 | 屏幕垂直居中 |
backgoundTapDismissEnable |
点击遮罩是否关闭弹窗 | NO |
alertViewEdging |
弹窗左右边距 | 15 |
backgroundView |
自定义遮罩视图 | 黑色 40% 蒙层 |
+showAlertViewWithView: |
一行代码弹出任意视图 | — |
+showAlertViewWithView:originY:backgoundTapDismissEnable: |
指定位置并允许点空白关闭 | — |
UIView 分类 API 速查表:showInWindow 家族
对新手最友好的入口是 UIView+TYAlertView 分类——任意 UIView(包括你从 XIB 加载的自定义视图)都能直接弹出。完整 API 见 UIView+TYAlertView.h:
Window 层弹出
| 方法 | 作用 |
|---|---|
showInWindow |
最简形式:居中弹出,点遮罩不关闭 |
showInWindowWithBackgoundTapDismissEnable: |
控制点击遮罩是否关闭 |
showInWindowWithOriginY: |
指定弹窗顶部 Y 坐标 |
showInWindowWithOriginY:backgoundTapDismissEnable: |
位置 + 遮罩关闭,全部控制 |
Controller 层弹出
| 方法 | 作用 |
|---|---|
showInController: |
默认 Alert 样式 + 淡入动画 |
showInController:preferredStyle: |
指定 TYAlertControllerStyleAlert 或 TYAlertControllerStyleActionSheet |
showInController:preferredStyle:transitionAnimation: |
额外指定 Fade / ScaleFade / DropDown / Custom 动画(枚举定义见 TYAlertController.h) |
隐藏与辅助
| 方法 | 作用 |
|---|---|
hideView |
智能判断当前在哪个层级显示,自动调用正确的关闭方法 |
hideInWindow / hideInController |
分别强制按 Window / Controller 方式关闭 |
createViewFromNib / createViewFromNibName: |
从 XIB 加载自定义弹窗视图(默认类名同名) |
viewController |
向上查找视图所属的 UIViewController |
最快上手:两行代码在 Window 层弹出弹窗
以库自带的 TYAlertView 为例,在 Window 层弹出一个带按钮的提示框只需两步(更多完整示例见 README.md 的 Usage 章节):
TYAlertView *alertView = [TYAlertView alertViewWithTitle:@"TYAlertView" message:@"全局提示"];
[alertView addAction:[TYAlertAction actionWithTitle:@"确定" style:TYAlertActionStyleDestructive handler:^(TYAlertAction *action) { }]];
// Window 层弹出:顶部距 200pt,点击遮罩关闭
[alertView showInWindowWithOriginY:200 backgoundTapDismissEnable:YES];
如果想用底层类,等价写法是 [TYShowAlertView showAlertViewWithView:alertView originY:200 backgoundTapDismissEnable:YES]。之后调用 hideView 或 hideInWindow 即可关闭,两种方式都能自动识别当前显示层级。
选型建议:什么时候用 Window 层?
- ✅ 需要跨页面常驻的提示(如登录态失效通知)→ 用
showInWindow - ✅ 不想依赖某个 ViewController 实例(比如在工具类、Model 里弹提示)→ 用
showInWindow - ✅ 需要 Alert / ActionSheet 标准样式、自定义过渡动画、生命周期回调(
viewDidShowHandler等)→ 用showInController(推荐方式) - ⚠️ 注意 Window 层弹出没有内置样式枚举和动画切换,视觉效果更简单
核心文件路径速查
- Window 层弹出容器:TYShowAlertView.h、TYShowAlertView.m
- UIView 分类入口:UIView+TYAlertView.h、UIView+TYAlertView.m
- Controller 层控制器与动画枚举:TYAlertController.h
- 模糊效果扩展:TYAlertController+BlurEffects.h
- 自定义弹窗示例(XIB + 代码):SettingModelView.h、ShareView.h
掌握 TYShowAlertView 的容器机制和 showInWindow 系列 API 后,你就能在 iOS 任意界面快速弹出一个全局置顶、支持点空白关闭的提示框了。
