工具栏中带有分段控件的macOS SwiftUI应用选项卡视图

时间:2019-10-12 03:09:35

标签: swift xcode macos swiftui

我正在尝试使用SwiftUI创建一个macOS应用。我需要TabView或类似的东西,但是当我使用TabView时,分段控件不在macOS工具栏中。 Click here to see an example of what I would like

我当前的代码是:

import SwiftUI

struct ContentView: View {
    var body: some View {
        TabView {
            Text("1")
                .tabItem {
                    Text("1")
            }
        }
    }
}

The result is here as an image

分段控件必须位于工具栏中,而不是视图中。

谢谢。

2 个答案:

答案 0 :(得分:2)

这里是实现此目的的可能方法的简化演示。经过测试并可以与Xcode 11.2一起使用。

demo

1)准备AppDelegate

中具有所需样式和背景的窗口
func applicationDidFinishLaunching(_ aNotification: Notification) {
    // Create the SwiftUI view that provides the window contents.
    let contentView = ContentView()
        .edgesIgnoringSafeArea(.top)
        .frame(minWidth: 480, maxWidth: .infinity, minHeight: 300, maxHeight: .infinity)

    // Create the window and set the content view. 
    window = NSWindow(
        contentRect: NSRect(x: 0, y: 0, width: 480, height: 300),
        styleMask: [.titled, .closable, .miniaturizable, .resizable, .fullSizeContentView],
        backing: .buffered, defer: false)
    window.center()
    window.titlebarAppearsTransparent = true
    window.titleVisibility = .hidden

    window.setFrameAutosaveName("Main Window")
    window.contentView = NSHostingView(rootView: contentView)
    window.makeKeyAndOrderFront(nil)
}

2)准备窗口内容视图以具有所需的行为

struct ContentView: View {
    private let tabs = ["Watch Now", "Movies", "TV Shows", "Kids", "Library"]
    @State private var selectedTab = 0
    var body: some View {
        VStack {
            HStack {
                Spacer()
                Picker("", selection: $selectedTab) {
                    ForEach(tabs.indices) { i in
                        Text(self.tabs[i]).tag(i)
                    }
                }
                .pickerStyle(SegmentedPickerStyle())
                .padding(.top, 8)
                Spacer()
            }
            .padding(.horizontal, 100)
            Divider()
            GeometryReader { gp in
                VStack {
                    ChildTabView(title: self.tabs[self.selectedTab], index: self.selectedTab)
                }
            }
        }
    }
}

struct ChildTabView: View {
    var title: String
    var index: Int

    var body: some View {
        Text("\(title)")
    }
}

答案 1 :(得分:0)

当我想在macOS BigSur上构建类似内容时,我偶然发现了您的问题。我正在使用 Xcode 12.2

这是我的解决方案的灵感来自Asperi的答案。重要的是,将窗口组的标题设置为空字符串“”,否则看起来很奇怪。

请注意,它仅在运行应用程序时有效,而在预览中不起作用!

TabBar Example

应用文件

import SwiftUI

@main
struct SegmentedToolbarApp: App {
    var body: some Scene {
        WindowGroup("") {
            ToobarItemPlacement()
        }
    }
}

ToobarItemPlacement视图

重要的部分是主要的展示位置。

设置更大的minWidth也很重要-否则工具栏将消失!

import SwiftUI

struct ToobarItemPlacement: View {
    
    private let tabs = ["Watch Now", "Movies", "TV Shows", "Kids", "Library"]
    @State private var selectedTab = 0
    
    var body: some View {
        VStack {
            ChildTabView(title: self.tabs[self.selectedTab], index: self.selectedTab)
        }
        .toolbar {
            ToolbarItem(placement: .principal) {
                
                Picker("", selection: $selectedTab) {
                    ForEach(tabs.indices) { i in
                        Text(self.tabs[i]).tag(i)
                    }
                }
                .pickerStyle(SegmentedPickerStyle())
                .padding(.top, 8)
            }
        }
        .frame(minWidth: 800, minHeight: 400)
    }
}

ChildTabView

struct ChildTabView: View {
    var title: String
    var index: Int

    var body: some View {
        Text("\(title) - Index \(index)")
            .padding()
    }
}