SystemNotification 是一个 SwiftUI SDK,它可以让你模仿原生 iOS 系统通知,比如当你切换静音模式、连接 AirPods 等时显示的通知。
系统通知可以进行样式设置和自定义。你可以使用看起来像原生的 SystemNotificationMessage
视图作为内容视图,或者使用任何自定义视图。
SystemNotification 可以通过 Swift Package Manager 安装
https://github.com/danielsaidi/SystemNotification.git
使用 SystemNotification,你可以像添加 sheet
、alert
和 fullScreenModal
一样,通过应用 systemNotification
视图修饰符(最好应用于应用程序的根视图)将系统通知添加到任何视图。
基于状态的通知需要一个布尔状态绑定和一个视图构建器。
import SystemNotification
struct MyView: View {
@State
var isActive = false
var body: some View {
VStack {
Button("Show notification") {
isActive = true
}
}
.systemNotification(isActive: $isActive) {
Text("You can use any custom content view")
.padding()
}
}
}
基于上下文的通知只需要一个 SystemNotificationContext
实例,然后可以使用单个修饰符显示许多不同的通知。
import SystemNotification
struct MyView: View {
@StateObject
var notification = SystemNotificationContext()
var body: some View {
VStack {
Button("Show text") {
notification.present {
Text("Context-based notifications are more flexible.")
.padding()
.multilineTextAlignment(.center)
}
}
Button("Show message") {
notification.present {
SystemNotificationMessage(
icon: Text("👍"),
title: "Great job!",
text: "You presented a native-looking message!"
)
}
}
}
.systemNotification(notification)
}
}
SystemNotificationMessage
视图让你能够轻松地模仿原生通知视图,包括图标、标题和文本,但你可以使用任何自定义视图作为通知主体。
请参阅在线入门指南以获取更多信息。
在线文档包含更多信息、文章、代码示例等。
Demo
文件夹中有一个应用程序,可让你探索该库。
你可以在 GitHub Sponsors 上赞助我,或者联系我以获得付费支持,以帮助支持我的开源项目。
你的支持使我能够投入更多精力到这些项目中,并使它们达到最佳状态。
如果您有任何问题或想以任何方式做出贡献,请随时与我联系
SystemNotification 在 MIT 许可证下可用。 有关更多信息,请参见LICENSE文件。