简体   繁体   English

SwiftUI & Mac 催化剂 | 向尾部导航栏添加按钮

[英]SwiftUI & Mac Catalyst | Adding buttons to trailing navigation bar

I have a Mac Catalyst app I had built using SwiftUI,and I cant seem to add buttons to the trailing navigation bar?我有一个使用 SwiftUI 构建的 Mac Catalyst 应用程序,我似乎无法向尾随导航栏添加按钮?

I am also unsure where this navigationBar is defined, is it possible to remove?我也不确定这个 navigationBar 是在哪里定义的,是否可以删除? It only seems to have appeared in Ventura.它似乎只出现在文图拉。

在此处输入图像描述

struct AppSidebarNavigation: View {
  
enum NavigationItem {
    case home
}

@State private var selection: NavigationItem? = .home

init() {

    #if !targetEnvironment(macCatalyst)
    UITableView.appearance().backgroundColor = UIColor(named: "White")
    UITableViewCell.appearance().selectionStyle = .none
    UITableView.appearance().allowsSelection = false

    #endif

}

var body: some View {
    NavigationView {
        sidebar
            .navigationTitle("")
            .navigationBarTitleDisplayMode(.inline)


        // Main View
        HomeView()
            .navigationTitle("")
            .edgesIgnoringSafeArea(.top)
            .navigationBarHidden(isMac ? false : true)
            .navigationBarBackButtonHidden(isMac ? false : true)
    }
    .accentColor(Color("Black"))
    .navigationViewStyle(.columns)

}
}

HomeView I had the following to the View. HomeView 我有以下视图。

#if targetEnvironment(macCatalyst)
.navigationBarItems(trailing:
    NavButtons
)
#endif

var NavButtons: some View {
    HStack {
        Button(action: { 
            Print("No")
        }) {
            Image(systemName: "plus")
                .font(.system(size: 14, weight: .medium))
        }
        .buttonStyle(NavPlusButton())
    }
}

I don't think it is possible to do that because:我认为不可能这样做,因为:

 @available(iOS, introduced: 13.0, deprecated: 100000.0, message: "Use toolbar(_:) with navigationBarLeading or navigationBarTrailing placement")
    @available(tvOS, introduced: 13.0, deprecated: 100000.0, message: "Use toolbar(_:) with navigationBarLeading or navigationBarTrailing placement")
    @available(macOS, unavailable)
    @available(watchOS, unavailable)
    public func navigationBarItems<T>(trailing: T) -> some View where T : View

This indicates on macOS the function in not available.这表示在 macOS 上 function 不可用。

I slightly modified your code (to get it to compile) and saw that where it would be is at the red circle below:我稍微修改了你的代码(让它编译)并看到它的位置在下面的红色圆圈处:

在此处输入图像描述

My code is:我的代码是:

struct AppSidebarNavigation: View {
    
    enum NavigationItem {
        case home
    }
    
    @State private var selection: NavigationItem? = .home
    
    var isMac: Bool
    
    init() {
        
#if !targetEnvironment(macCatalyst)
        UITableView.appearance().backgroundColor = UIColor(named: "White")
        UITableViewCell.appearance().selectionStyle = .none
        UITableView.appearance().allowsSelection = false
        isMac = false
#else
        isMac = true
#endif
        
    }
    
    var body: some View {
        NavigationView {

            // Main View
            HomeView()
                .navigationTitle("NavTitle")
                .edgesIgnoringSafeArea(.top)
                .navigationBarHidden(isMac ? false : true)
                .navigationBarBackButtonHidden(isMac ? false : true)
                .navigationBarItems(trailing: NavButtons)
        }
        .accentColor(Color("Black"))
        .navigationViewStyle(.columns)
        
    }
}
var NavButtons: some View {
    HStack {
        Button(action: {}) {
            Image(systemName: "plus")
        }
//        Button(action: {
//            print("No")
//        }) {
//            Image(systemName: "plus")
//                .font(.system(size: 14, weight: .medium)).frame(width: 80)
//        }
    }
}

struct HomeView: View {
    var body: some View {
        Group {
            Text("Home View")
            NavButtons
        }
    }
}

struct HomeView_Previews: PreviewProvider {
    static var previews: some View {
        HomeView()
    }
}

struct NavButton_Previews: PreviewProvider {
    static var previews: some View {
        NavButtons
    }
}
struct ContentView: View {
    var body: some View {
        VStack {
            Image(systemName: "globe")
                .imageScale(.large)
                .foregroundColor(.accentColor)
            Text("Hello, world!")
            AppSidebarNavigation()
        }
        .padding()
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

You can use Toolbar modifier with .primaryAction as placement to place button on the trailing side on Navigation Bar .您可以将Toolbar修饰符与.primaryAction用作将按钮放置在Navigation Bar尾端的位置。

Just replace .navigationBarItem with below:只需将.navigationBarItem替换为以下内容:

#if targetEnvironment(macCatalyst)
    .toolbar {
        ToolbarItem(placement: .primaryAction) {
            NavButtons
        }
    }
#endif

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM