以自定义样式显示 SwiftUI 视图

SwiftUIPresent

呈现来自 SwiftUI 的视图,具有自定义样式,扩展到 sheet 和 fullscreenCover 之外。

特点

  • 将缺失的淡入淡出(crossDissolve)和弹出窗口演示样式添加到 SwiftUI。
  • (WIP)将 SwiftUI sheet 的高度调整(detents)API(仅适用于 iOS 16)引入 iOS 15。
  • 制作自己的演示样式并在 SwiftUI 中使用它。

要求

  • iOS 14.0+(iOS 13 以上)
  • Xcode 14.3+

安装

Swift 包管理器

使用 Apple 发布的教程,使用当前版本的 SwiftUIPresent 仓库 URL。

https://github.com/Lumisilk/SwiftUIPresent.git

用法

使用 present(isPresented: Binding<Bool>, style: some PresentationStyle)

import SwiftUIPresent

struct Example: View {
    @State private var isPresented = false

    var body: some View {
        Button("Present") {
            isPresented = true
        }
        .present(isPresented: $isPresented, style: .fade) {
            Text("Your present content")
        }
    }
}

内置样式

PresentationStyle 相应的 UIKit 样式
.sheet .formSheet
.fade(backgroundColor: UIColor = .clear) .overFullScreen × .crossDissolve
.popover(backgroundColor: UIColor? = nil) (backgroundColor 表示气泡的背景颜色,你通常不会改变它) .popover

创建自己的样式

符合 PresentationStyle,提供你自己的 UIViewController 实现。

(稍后将添加更多解释)

限制

  • withAnimation(_:_:) 对演示内容没有效果。请改用 animation(_:value:) 修饰符。

路线图

  • 使用 iOS 15 的新 API 支持 sheet detent 自定义。
  • 添加可选数据绑定以控制演示。
  • 支持像首选项和环境一样隐式传递值。
  • 添加文档
  • 支持 iOS 13+

GitHub

点击跳转