TYAlertController新手教程:5行代码实现优雅AlertView与ActionSheet弹窗

原创2026-10-14 21:40:121,345 阅读

TYAlertController新手教程:5行代码实现优雅AlertView与ActionSheet弹窗

TYAlertController 是一款免费、易用的 iOS Objective-C 弹窗组件,几行代码即可在 controller 或 window 上弹出优雅的 AlertView 和 ActionSheet,原生支持毛玻璃模糊效果、自定义视图与转场动画,同时适配 iPhone 和 iPad。本文将从安装到进阶特性,带你快速上手。

TYAlertController iOS弹窗组件演示界面,展示AlertView弹窗所在屏幕的背景效果

为什么选择 TYAlertController?📱

特性 说明
双形态 AlertView 居中弹窗 / ActionSheet 底部操作栏
模糊效果 一行代码给背景加毛玻璃
自定义视图 任意 UIView(含 Xib)直接弹出
转场动画 Fade / ScaleFade / DropDown 内置三种,可扩展
展示灵活 支持 controller 与 window 两种层级
轻量 纯 Objective-C,AutoLayout 布局,支持 ARC

环境要求:Xcode 5 以上、iOS 7.0 以上,详见 README.md。

第一步:一键安装 TYAlertController 🚀

使用 CocoaPods 导入最省心:

pod 'TYAlertController'

安装完成后,引入分类头文件即可开始使用:

#import "UIView+TYAlertView.h"

如需模糊效果,再引入 TYAlertController+BlurEffects.h 即可。

第二步:5行代码弹出 AlertView ⚡

TYAlertView *alertView = [TYAlertView alertViewWithTitle:@"提示" message:@"确定要提交吗?"];
[alertView addAction:[TYAlertAction actionWithTitle:@"确定" style:TYAlertActionStyleDestructive handler:^(TYAlertAction *action) { /* 确定逻辑 */ }]];
[alertView addAction:[TYAlertAction actionWithTitle:@"取消" style:TYAlertActionStyleCancel handler:^(TYAlertAction *action) { /* 取消逻辑 */ }]];
TYAlertController *alertController = [TYAlertController alertControllerWithAlertView:alertView preferredStyle:TYAlertControllerStyleAlert];
[self presentViewController:alertController animated:YES completion:nil];

弹窗就弹出来了!几个实用小点:

  • 调用 addTextFieldWithConfigurationHandler: 可追加输入框,通过 textFieldArray 读取输入内容(block 中建议弱引用,避免循环引用)
  • backgoundTapDismissEnable = YES 允许点按背景关闭
  • alertViewOriginY 调整弹窗垂直位置
  • 提供 viewWillShowHandler、dismissComplete 等生命周期回调

第三步:一行切换 ActionSheet 形态 📋

把样式参数改为 TYAlertControllerStyleActionSheet,同一个弹窗就变成底部操作栏:

TYAlertController *alertController = [TYAlertController alertControllerWithAlertView:alertView preferredStyle:TYAlertControllerStyleActionSheet];

按钮支持三种样式:TYAlertActionStyleDefault、TYAlertActionStyleCancel、TYAlertActionStyleDestructive,可直观区分常规操作、取消与破坏性操作(如删除)。

进阶玩法:模糊效果与自定义视图 ✨

模糊效果:拿到 TYAlertController 后调用 setBlurEffectWithView:self.view,即可获得熟悉的毛玻璃背景,实现见 UIImage+ImageEffects.m。

自定义视图:任何 UIView 都能直接弹出。示例工程中的分享面板 ShareView、设置面板 SettingModelView 就是由 Xib 创建后原样弹出的,样式完全由你掌控。

三种转场动画,即改即用 🎬

动画同样通过参数选择,改一行代码即可切换:

TYAlertController *alertController = [TYAlertController alertControllerWithAlertView:alertView
                                                          preferredStyle:TYAlertControllerStyleAlert
                                                     transitionAnimation:TYAlertTransitionAnimationDropDown];
  • TYAlertTransitionAnimationFade:淡入淡出
  • TYAlertTransitionAnimationScaleFade:缩放 + 淡入
  • TYAlertTransitionAnimationDropDown:从顶部掉落

想要独特效果?继承 TYBaseAnimation.h 实现自己的动画类即可。

没有 Controller?在 Window 上直接弹 🪟

需要在 App 任意位置弹窗(没有合适的 controller 时),UIView+TYAlertView.h 分类提供了 window 级展示:

[alertView showInWindowWithOriginY:200 backgoundTapDismissEnable:YES];

核心文件速览 📂

文件 说明
TYAlertController.h 弹窗控制器,样式与动画枚举定义
TYAlertView.h 弹窗视图与按钮 TYAlertAction
UIView+TYAlertView.h 一行展示分类(controller / window)
ViewController.m 示例工程完整用法演示
TYAlertController.podspec CocoaPods 配置

官方 README.md 中还有更多细节,欢迎对照着边看边试。


总结:TYAlertController 用 CocoaPods 一行安装、5 行代码弹出美观弹窗,还附带模糊效果、自定义视图与转场动画,是 iOS 弹窗场景中非常值得收藏的轻量工具。

登录后查看全文
TYAlertController