【问题标题】:SwiftUI: How to show some toolbar buttons only for iPhoneSwiftUI:如何只为 iPhone 显示一些工具栏按钮
【发布时间】:2021-05-10 17:58:11
【问题描述】:

我有一组工具栏按钮,只有在设备是 iPhone(而不是 iPad)时才会显示。

以下代码因此错误而失败:

包含控制流语句的闭包不能与结果生成器“ToolbarContentBuilder”一起使用

我明白它为什么会失败,但我无法想出一个解决方案来满足我的需要。

import SwiftUI

struct ContentView: View {
    var body: some View {
        NavigationView {
            List {
                NavigationLink(
                    destination: Hello(),
                    label: {
                        Text("Hello")
                    })
            }
        }
    }
}

struct Hello: View {
    var body: some View {
        Text("Hello World")
            .toolbar() {
                if UIDevice.current.userInterfaceIdiom == .phone {
                    ToolbarItem(placement: .navigationBarTrailing) {
                        Button(action: {
                            // do something
                        }, label: {
                            Text("Button1")
                        })
                    }
                    ToolbarItem(placement: .navigationBarTrailing) {
                        Button(action: {
                            // do something
                        }, label: {
                            Text("Button2")
                        })
                    }
                }
                ToolbarItem(placement: .navigationBarTrailing) {
                    Button(action: {
                        // do something
                    }, label: {
                        Text("Button3")
                    })
                }
            }
    }
}

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

我很高兴创建一个单独的函数来实现它。我根本不知道该怎么做。

【问题讨论】:

    标签: swiftui toolbaritems swiftui-view


    【解决方案1】:

    也许你想要这个

    struct Hello: View {
        var body: some View {
            Text("Hello World")
                .toolbar() {
                    ToolbarItemGroup(placement: .navigationBarTrailing) {
                        if UIDevice.current.userInterfaceIdiom == .phone {
                            Button(action: {
                                // do something
                            }, label: {
                                Text("Button1")
                            })
                            Button(action: {
                                // do something
                            }, label: {
                                Text("Button2")
                            })
                        }
                        Button(action: {
                            // do something
                        }, label: {
                            Text("Button3")
                        })
                    }
                }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-05-02
      • 1970-01-01
      • 2022-10-05
      • 1970-01-01
      • 1970-01-01
      • 2019-09-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多