swift - 添加新窗口并使其成为 SWIFTUI 中的关键窗口

标签 swift swiftui swiftui-navigationlink

我想添加一个新窗口,因为我想创建一个全屏加载程序。我尝试添加一个新窗口并将其设置为 rootviewcontroller。但它并没有添加到 Windows 层次结构中。下面是我的代码。我正在学习 swiftUI。任何帮助表示赞赏。

let window = UIWindow()
window.frame = CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.height)
window.backgroundColor = .blue
window.isHidden = false
window.rootViewController = UIHostingController(rootView: Text("Loading...."))
window.makeKeyAndVisible()

最佳答案

您需要包装 UIActivityIndi​​cator 并使其成为 UIViewRepresentable。

struct ActivityIndicator: UIViewRepresentable {

@Binding var isAnimating: Bool
style: UIActivityIndicatorView.Style

func makeUIView(context: UIViewRepresentableContext<ActivityIndicator>) -> UIActivityIndicatorView {
    return UIActivityIndicatorView(style: style)
}

func updateUIView(_ uiView: UIActivityIndicatorView, context: UIViewRepresentableContext<ActivityIndicator>) {
    isAnimating ? uiView.startAnimating() : uiView.stopAnimating()
  }
}

然后您可以按如下方式使用它 - 这是加载叠加层的示例。

注意:我更喜欢使用ZStack,而不是overlay(:_),所以我确切地知道是什么 我的实现中正在进行 struct LoadingView:查看内容:查看{

@Binding var isShowing: Bool
var content: () -> Content

var body: some View {
    GeometryReader { geometry in
        ZStack(alignment: .center) {

            self.content()
                .disabled(self.isShowing)
                .blur(radius: self.isShowing ? 3 : 0)

            VStack {
                Text("Loading...")
                ActivityIndicator(isAnimating: .constant(true), style: .large)
            }
            .frame(width: geometry.size.width / 2,
                   height: geometry.size.height / 5)
            .background(Color.secondary.colorInvert())
            .foregroundColor(Color.primary)
            .cornerRadius(20)
            .opacity(self.isShowing ? 1 : 0)

         }
      }
   }

}

关于swift - 添加新窗口并使其成为 SWIFTUI 中的关键窗口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59578434/

相关文章:

SwiftUI:如何横向呈现 FullScreenCover

swiftui - 从 SwiftUI 中的导航堆栈中删除屏幕

swift - 带有协议(protocol)对象的纯 swift 集

ios - 在 Firebase 3 中重新使用访问 token

macos - 使用键盘快捷键关注 TextField

swift - SwiftUI 中的预览真的需要#if DEBUG 语句才能在发布版本中将其删除吗?

ios - 在主线程上链接 UITableview 更新

ios - UIView 水龙头识别器不工作

swift - 列表选择为 Set<String> - 如何使用?

SwiftUi - 隐藏 "Back"按钮和导航栏(出现几分之一秒)