2

我想添加一个新窗口,因为我想创建一个全屏加载程序。我试图在其中添加一个新窗口并将其设置为 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()
4

2 回答 2

1

您需要包装 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: View where Content: View {

@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)

         }
      }
   }

}
于 2020-01-03T13:10:45.513 回答
1

如果要显示备用窗口,则必须将新窗口场景连接UIWindow到现有窗口场景,因此这里有一个SceneDelegate基于发布通知的可能方法的演示。

// notification names declarations
let showFullScreenLoader = NSNotification.Name("showFullScreenLoader")
let hideFullScreenLoader = NSNotification.Name("hideFullScreenLoader")

// demo alternate window
struct FullScreenLoader: View {
    var body: some View {
        VStack {
            Spacer()
            Button("Close Loader") {
                NotificationCenter.default.post(name: hideFullScreenLoader, object: nil)
            }
        }
    }
}

// demo main window
struct MainView: View {
    var body: some View {
        VStack {
            Button("Show Loader") {
                NotificationCenter.default.post(name: showFullScreenLoader, object: nil)
            }
            Spacer()
        }
    }
}


class SceneDelegate: UIResponder, UIWindowSceneDelegate {

    var window: UIWindow? // << main window
    var loaderWindow: UIWindow?  // << alternate window

    private var subscribers = Set<AnyCancellable>()

    func makeAntherWindow() { // << alternate window creation
        if let windowScene = window?.windowScene {
            let newWindow = UIWindow(windowScene: windowScene)
            let contentView = FullScreenLoader()
            newWindow.rootViewController = UIHostingController(rootView: contentView)

            self.loaderWindow = newWindow
            newWindow.makeKeyAndVisible()
        }
    }

    override init() {
        super.init()
        NotificationCenter.default.publisher(for: hideFullScreenLoader)
            .sink(receiveValue: { _ in
                self.loaderWindow = nil // remove alternate window
            })
            .store(in: &self.subscribers)
        NotificationCenter.default.publisher(for: showFullScreenLoader)
            .sink(receiveValue: { _ in
                self.makeAntherWindow() // create alternate window
            })
            .store(in: &self.subscribers)
    }

    func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {

        let contentView = MainView()
        if let windowScene = scene as? UIWindowScene {
            let window = UIWindow(windowScene: windowScene)
            window.rootViewController = UIHostingController(rootView: contentView)
            self.window = window
            window.makeKeyAndVisible()

        }
    }
    ...
于 2020-01-03T13:21:05.270 回答