JDStatusBarNotification 使用指南:iOS状态栏通知组件详解
2025-07-08 08:04:09作者:劳婵绚Shirley
组件概述
JDStatusBarNotification 是一款轻量级、高度可定制的 iOS 状态栏通知组件,它允许开发者在应用顶部状态栏区域展示各种形式的通知信息。该组件支持 Swift 和 Objective-C 双语言调用,提供丰富的展示效果和交互方式。
基础使用
SwiftUI 集成方式
1. 基础文本通知
var body: some View {
Button("显示/隐藏通知") {
isPresented.toggle()
}
.notification(title: "欢迎使用", isPresented: $isPresented)
}
这种方式适合简单的文本提示场景,通过绑定布尔值控制通知的显示与隐藏。
2. 带副标题和状态指示的通知
var body: some View {
Button("显示高级通知") {
isPresented.toggle()
}
.notification(title: "操作进行中",
subtitle: "请稍候...",
isPresented: $isPresented,
isShowingActivity: $activity, // 显示加载指示器
progress: $progress, // 显示进度条
includedStyle: .success) // 使用预定义成功样式
}
这种形式适合需要展示更多信息的场景,如操作状态、进度反馈等。
3. 自定义视图通知
var body: some View {
Button("显示自定义视图") {
isPresented.toggle()
}
.notification(isPresented: $isPresented) {
VStack {
Text("👋 自定义通知")
.font(.headline)
ProgressView()
}
.foregroundStyle(.white)
}
}
当预置样式无法满足需求时,可以使用完全自定义的 SwiftUI 视图。
编程式调用(支持 Swift/Objective-C)
1. 基础通知展示
// 简单文本通知
NotificationPresenter.shared.present("操作已完成")
// 带回调的展示
NotificationPresenter.shared.present("正在处理...") { presenter in
// 通知显示后的处理逻辑
}
2. 通知控制方法
// 立即隐藏通知
NotificationPresenter.shared.dismiss()
// 延迟隐藏
NotificationPresenter.shared.dismiss(after: 2.0) { presenter in
// 隐藏完成后的回调
}
3. 添加交互元素
// 显示加载指示器
NotificationPresenter.shared.present("正在加载")
NotificationPresenter.shared.displayActivityIndicator(true)
// 添加左侧图标
let icon = UIImageView(image: UIImage(systemName: "wifi"))
NotificationPresenter.shared.present("网络连接")
NotificationPresenter.shared.displayLeftView(icon)
4. 进度展示
// 动画进度条
NotificationPresenter.shared.present("上传中...") { presenter in
presenter.animateProgressBar(to: 1.0, duration: 2.0) { presenter in
presenter.dismiss()
}
}
// 静态进度设置
NotificationPresenter.shared.displayProgressBar(at: 0.5)
样式定制
1. 使用预设样式
组件内置了多种常用样式:
NotificationPresenter.shared.present("操作成功",
includedStyle: .success)
NotificationPresenter.shared.present("发生错误",
includedStyle: .error)
NotificationPresenter.shared.present("警告信息",
includedStyle: .warning)
2. 完全自定义样式
SwiftUI 方式:
.notification(isPresented: $isPresented, style: {
var style = $0
style.backgroundStyle.backgroundColor = .blue
style.textStyle.font = .systemFont(ofSize: 14, weight: .bold)
style.pillStyle.cornerRadius = 10
return style
}) {
// 自定义视图内容
}
编程式设置:
// 修改默认样式
NotificationPresenter.shared.updateDefaultStyle { style in
style.backgroundStyle.backgroundColor = .systemIndigo
style.textStyle.textColor = .white
style.subtitleStyle.font = .systemFont(ofSize: 12)
return style
}
// 创建命名样式
NotificationPresenter.shared.addStyle(named: "customAlert") { style in
style.backgroundStyle.backgroundColor = .red
style.textStyle.textColor = .white
return style
}
// 使用命名样式
NotificationPresenter.shared.present("紧急通知", styleName: "customAlert")
高级功能
1. 自定义视图支持
SwiftUI 视图:
NotificationPresenter.shared.presentSwiftView {
HStack {
Image(systemName: "bell.fill")
Text("新消息")
}
.foregroundColor(.white)
}
UIKit 视图:
let customView = MyCustomNotificationView()
NotificationPresenter.shared.presentCustomView(customView)
2. 响应系统外观变化
组件自动支持 Light/Dark 模式切换,也可以手动设置不同模式下的样式:
NotificationPresenter.shared.updateDefaultStyle { style in
style.backgroundStyle.backgroundColor = UIColor { trait in
return trait.userInterfaceStyle == .dark ? .black : .white
}
return style
}
最佳实践建议
- 通知时长控制:简单提示建议2秒左右,重要信息可适当延长
- 优先级管理:避免频繁显示多个通知造成干扰
- 样式一致性:为应用定义统一的通知样式规范
- 无障碍支持:确保通知文本有足够的对比度
- 性能优化:复用样式对象,避免频繁创建
JDStatusBarNotification 通过简洁的 API 和强大的定制能力,能够满足从简单提示到复杂交互的各种通知场景需求,是提升 iOS 应用用户体验的有效工具。