问题描述
我正在尝试为我的macOS SwiftUI状态栏应用程序创建设置视图.到目前为止,我的实现一直使用 NavigationView
和 NavigationLink
,但是当设置视图将父视图推向侧面时,此解决方案将产生一个半视图.下面的屏幕截图和代码示例.
I'm attempting to create a settings view for my macOS SwiftUI status bar app. My implementation so far has been using a NavigationView
, and NavigationLink
, but this solution produces a half view as the settings view pushes the parent view to the side. Screenshot and code example below.
导航侧边栏
struct ContentView: View {
var body: some View {
VStack{
NavigationView{
NavigationLink(destination: SecondView()){
Text("Go to next view")
}}
}.frame(width: 800, height: 600, alignment: .center)}
}
struct SecondView: View {
var body: some View {
VStack{
Text("This is the second view")
}.frame(width: 800, height: 600, alignment: .center)
}
}
我所能找到的少量信息表明,在macOS上使用SwiftUI不可避免,因为iOS( StackNavigationViewStyle
)上的'全屏' NavigationView
The little information I can find suggests that this is unavoidable using SwiftUI on macOS, because the 'full screen' NavigationView
on iOS (StackNavigationViewStyle
) is not available on macOS.
在SwiftUI for macOS中,是否存在一种简单甚至复杂的方法来实现向设置视图的过渡,而该视图占据了整个框架?如果不是,是否可以使用 AppKit
调用用SwiftUI编写的View对象?
Is there a simple or even complex way of implementing a transition to a settings view that takes up the whole frame in SwiftUI for macOS? And if not, is it possible to use AppKit
to call a View object written in SwiftUI?
也是Swift的新手-请保持温柔.
Also a Swift newbie - please be gentle.
推荐答案
以下是自定义类导航解决方案的可能方法的简单演示.使用Xcode 11.4/macOS 10.15.4进行了测试
Here is a simple demo of possible approach for custom navigation-like solution. Tested with Xcode 11.4 / macOS 10.15.4
注意:使用背景色可提高可见度.
struct ContentView: View {
@State private var show = false
var body: some View {
VStack{
if !show {
RootView(show: $show)
.frame(maxWidth: .infinity, maxHeight: .infinity)
.background(Color.blue)
.transition(AnyTransition.move(edge: .leading)).animation(.default)
}
if show {
NextView(show: $show)
.frame(maxWidth: .infinity, maxHeight: .infinity)
.background(Color.green)
.transition(AnyTransition.move(edge: .trailing)).animation(.default)
}
}
}
}
struct RootView: View {
@Binding var show: Bool
var body: some View {
VStack{
Button("Next") { self.show = true }
Text("This is the first view")
}
}
}
struct NextView: View {
@Binding var show: Bool
var body: some View {
VStack{
Button("Back") { self.show = false }
Text("This is the second view")
}
}
}
这篇关于单一视图的macOS SwiftUI导航的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!