JDStatusBarNotification 使用指南:iOS状态栏通知组件快速入门
JDStatusBarNotification 项目地址: https://gitcode.com/gh_mirrors/jds/JDStatusBarNotification
组件概述
JDStatusBarNotification 是一个轻量级的 iOS 状态栏通知组件,它允许开发者在应用顶部状态栏区域显示各种通知信息。与传统的弹窗或 Toast 通知相比,这种设计更加优雅且不打断用户操作流程。
基础功能使用
1. 显示文本通知
这是最基本的使用方式,适合显示简单的文本信息:
// 简单文本通知
NotificationPresenter.shared.present("操作已完成")
// 带副标题的通知
NotificationPresenter.shared.present("新消息", subtitle: "来自张三")
// 带完成回调的通知
NotificationPresenter.shared.present("数据加载中...") { presenter in
print("通知已显示")
}
2. 显示 SwiftUI 视图通知
对于使用 SwiftUI 的项目,可以直接嵌入 SwiftUI 视图:
NotificationPresenter.shared.presentSwiftView {
HStack {
Image(systemName: "wifi")
Text("网络已连接")
}
.foregroundColor(.blue)
}
3. 通知的隐藏控制
// 立即隐藏
NotificationPresenter.shared.dismiss()
// 延迟隐藏(0.5秒后)
NotificationPresenter.shared.dismiss(after: 0.5)
// 带完成回调的隐藏
NotificationPresenter.shared.dismiss { presenter in
print("通知已隐藏")
}
高级功能展示
1. 活动指示器
适合需要显示加载状态的场景:
NotificationPresenter.shared.present("正在同步数据...")
NotificationPresenter.shared.displayActivityIndicator(true)
// 完成后
NotificationPresenter.shared.displayActivityIndicator(false)
2. 自定义左侧视图
可以在通知左侧添加图标等自定义视图:
let icon = UIImageView(image: UIImage(systemName: "cloud.rain.fill"))
NotificationPresenter.shared.present("天气预警", subtitle: "暴雨橙色预警")
NotificationPresenter.shared.displayLeftView(icon)
3. 进度条显示
适合文件下载、上传等需要显示进度的场景:
NotificationPresenter.shared.present("文件上传中...") { presenter in
presenter.animateProgressBar(to: 1.0, duration: 2.0) { presenter in
presenter.dismiss()
}
}
预设样式与自定义
1. 使用内置样式
组件提供了几种常用预设样式:
// 成功样式
NotificationPresenter.shared.present("操作成功", includedStyle: .success)
// 错误样式
NotificationPresenter.shared.present("操作失败", includedStyle: .error)
// 警告样式
NotificationPresenter.shared.present("警告", includedStyle: .warning)
2. 完全自定义样式
可以深度定制通知的各个方面:
// 修改默认样式
NotificationPresenter.shared.updateDefaultStyle { style in
style.backgroundStyle.backgroundColor = UIColor.systemIndigo
style.textStyle.textColor = UIColor.white
style.textStyle.font = UIFont.boldSystemFont(ofSize: 14)
style.subtitleStyle.textColor = UIColor.lightText
style.progressBarStyle.barColor = UIColor.white
style.progressBarStyle.height = 2.0
return style
}
// 创建命名样式
NotificationPresenter.shared.addStyle(named: "customAlert") { style in
style.backgroundStyle.backgroundColor = UIColor.red
style.textStyle.textColor = UIColor.white
style.textStyle.font = UIFont.systemFont(ofSize: 15, weight: .medium)
return style
}
// 使用命名样式
NotificationPresenter.shared.present("紧急通知", styleName: "customAlert")
最佳实践建议
-
通知时长控制:普通信息建议显示2-3秒,重要信息可适当延长但不超过5秒
-
内容简洁:主文本建议不超过15个汉字,副文本不超过25个汉字
-
样式一致性:应用中应保持通知样式的统一,避免频繁变化
-
优先级管理:重要通知应暂停当前通知立即显示,非重要通知可排队显示
-
暗黑模式适配:自定义样式时需考虑暗黑模式的适配
JDStatusBarNotification 通过简洁的 API 和强大的自定义能力,为 iOS 应用提供了优雅的状态栏通知解决方案。开发者可以根据实际需求灵活运用基础功能和高级特性,打造符合应用风格的通知系统。
JDStatusBarNotification 项目地址: https://gitcode.com/gh_mirrors/jds/JDStatusBarNotification
创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考