我正在试验一个“纯”的 SwiftUI 应用程序。它没有,SceneDelegate所以我不确定在 iOS 上运行时我需要的 Hosting Controller 东西放在哪里。
以前在SceneDelegate我有代码会说这样的话:
let contentView = ContentView()
window.rootViewController = UIHostingController(rootView: contentView)
Run Code Online (Sandbox Code Playgroud)
现在我只有一个@main文件:
var body: some Scene {
WindowGroup {
ContentView()
}
}
Run Code Online (Sandbox Code Playgroud)
那么 Hosting Controller 的东西去哪里了(或者我还能如何访问 SwiftUI 没有的 UIKit 功能?(具体来说,我想弄乱状态栏,自动隐藏主页指示器,以及一些关于亮/暗的事情) SwiftUIpreferredColorScheme不涵盖的模式。)
情况
实现一个多窗口应用程序,其中每个窗口都有自己的状态。
例子
这是一个展示问题的示例(在 github 上):
import SwiftUI
@main
struct multi_window_menuApp: App {
var body: some Scene {
WindowGroup {
ContentView()
}.commands {
MenuCommands()
}
}
}
struct ContentView: View {
@StateObject var viewModel: ViewModel = ViewModel()
var body: some View {
TextField("", text: $viewModel.inputText)
.disabled(true)
.padding()
}
}
public class ViewModel: ObservableObject {
@Published var inputText: String = "" {
didSet {
print("content was updated...")
}
}
}
Run Code Online (Sandbox Code Playgroud)
问题
我们应该如何以编程方式找出当前选定的视图是什么,以便我们可以在菜单命令即将完成时更新状态并更新视图模型中的状态?
import Foundation
import SwiftUI
import Combine
struct MenuCommands: …Run Code Online (Sandbox Code Playgroud) 在这个答案中,该解决方案适用于 Scene plus swiftUI。
但是使用@main 就像:
@main
struct MyApp: App {
@StateObject private var model = MyModel()
var body: some Scene {
WindowGroup {
Router {
AppContent()
}.environmentObject(self.model)
}
}
}
Run Code Online (Sandbox Code Playgroud)
我还尝试通过使用获取主窗口
var window: NSWindow? {
let window = NSApplication.shared.mainWindow
return window
}
Run Code Online (Sandbox Code Playgroud)
尽管如此,mainWindow总是返回nil
我需要NSWindow由于需要符合ASWebAuthenticationPresentationContextProviding哪些有义务返回NSWindow. 基本上,我正在尝试执行以下操作:
LoginView(store: AuthStore(window: window))
Run Code Online (Sandbox Code Playgroud)
WhereAuthStore用于AuthenticationServices执行身份验证。
因此,我在我的应用程序中支持三个主题,每个主题都有不同的色调颜色。我正在使用 @EnvironmetObject 来跟踪更改。但是,我无法在 SceneDelegate.swift 文件上使用它,因为应用程序崩溃了。此外,accentColor不是一个选项,因为它不会改变警报tintColor。我该怎么做?
这是一些代码:
SceneDelegate.swift 文件
class SceneDelegate: UIResponder, UIWindowSceneDelegate {
@EnvironmentObject var userData: UserData
var window: UIWindow?
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
// Use this method to optionally configure and attach the UIWindow `window` to the provided UIWindowScene `scene`.
// If using a storyboard, the `window` property will automatically be initialized and attached to the scene.
// This delegate does not imply the connecting scene or session are new (see `application:configurationForConnectingSceneSession` …Run Code Online (Sandbox Code Playgroud) 我想制作一个视差背景视图,当窗口在屏幕上移动时,UI 后面的图像几乎保持静止。要在 macOS 上执行此操作,我想获取窗口的坐标。我如何获得窗口的坐标?
我问这个是因为我找不到任何说明如何做到这一点的地方:
谷歌搜索帮助我找到了以下结果:
苹果开发者文档:
GeometryReader - 我曾希望这将包含一个 API 来为我提供系统坐标空间中的框架,但似乎所有方法都只包含参考窗口内坐标其他问题:
(0, …