툴바에 세그먼트 컨트롤이있는 macOS SwiftUI 앱 탭보기


9

SwiftUI로 macOS 앱을 만들려고합니다. 나 TabView또는 비슷한 것이 필요 하지만 TabView세그먼트 컨트롤을 사용할 때 macOS 툴바에 없습니다. 내가 원하는 것의 예를 보려면 여기를 클릭하십시오

내 현재 코드는 다음과 같습니다

import SwiftUI

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

결과는 여기 이미지로

세그먼트 컨트롤은 뷰가 아닌 도구 모음에 있어야합니다.

감사합니다.

답변:


1

다음은이를 달성하기위한 가능한 접근 방식의 단순화 된 데모입니다. Xcode 11.2에서 테스트 및 작동합니다.

데모

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)")
    }
}

답변 주셔서 감사합니다. AppKit에 있더라도 Apple의 앱과 비슷한 것을 찾고 있습니다. 귀하의 답변에 감사드립니다. 감사합니다
— NG235

애플은 WWDC 2019에서 툴바에 세그먼트 컨트롤이있는 SwiftUI Mac 앱을 보여 주었다고 생각합니다.
— NG235
당사 사이트를 사용함과 동시에 당사의 쿠키 정책과 개인정보 보호정책을 읽고 이해하였음을 인정하는 것으로 간주합니다.
Licensed under cc by-sa 3.0 with attribution required.