【问题标题】:How can I remove the toggle button in Swiftui and how can I show a sidebar on the iPad when this is shown high up如何删除 Swiftui 中的切换按钮以及如何在 iPad 上显示侧边栏
【发布时间】:2021-05-15 21:11:04
【问题描述】:

我希望在 iPad 上显示一个侧边栏。然而,让我对 Swiftui Navigazion View 感到困扰的是我有一个丑陋的切换按钮。此外,我想在 iPad 水平放置时显示一个侧边栏。我可以更改导航视图组件以使其正常工作吗?

【问题讨论】:

  • 您找到解决方案了吗?

标签: swiftui swiftui-navigationview


【解决方案1】:

不,但您可以自定义构建自己的:

struct ContentView: View {
    
    @State private var selection: Int? = nil
    
    var body: some View {
        
        HStack {
            List {
                Button {  selection = 1
                } label: {
                    Text("Item 1")
                }
                
                Button {  selection = 2
                } label: {
                    Text("Item 2")
                }
                
                Button {  selection = 3
                } label: {
                    Text("Item 3")
                }
            }
            .frame(width: 200)
            .frame(maxHeight: .infinity)
            .background(.gray.opacity(0.3))
            
            VStack {
                if selection != nil {
                    // Detail View
                    Text("Your detail view \(selection!)")
                        .font(.title)
                } else {
                    Text("Select an item")
                }
            }
            .frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .center)
        }
    }
}

【讨论】:

    【解决方案2】:

    然而,我对 Swiftui Navigazion View 的困扰是我有一个丑陋的切换按钮

    避免按钮的可能解决方法是隐藏导航栏,然后在横向(也称为水平)中,您只会看到侧边栏

        NavigationView {
            VStack {
                Text("Header")
                    .padding()
                List(0..<100, id: \.self) { i in
                    NavigationLink(
                        tag: i,
                        selection: $activeLink,
                        destination: { Text("Details for \(i)") }
                    ) {
                        Text("Row #\(i)")
                    }
                }
            }
            .navigationBarHidden(true)     // << here !!
    
            Text("Default Details")
        }
    

    【讨论】:

      猜你喜欢
      • 2021-10-18
      • 1970-01-01
      • 2021-07-14
      • 1970-01-01
      • 2021-02-04
      • 1970-01-01
      • 2020-08-29
      • 2021-03-21
      • 2011-06-09
      相关资源
      最近更新 更多