开发者社区> 问答> 正文

SwiftUI无法删除Picker-Form版本上方的空间

SwiftUI无法删除Picker-Form版本上的空间

在SwiftUI中,我正在为选择器的格式而苦苦挣扎。我在一个视图应用程序中构建了一个带有其他几个视图的简单选择器。在标题和选择器之间有一个我无法删除的空间。我宁愿把灰色设置成白色。如果我改变了选择器的框架,它只会收缩选择器-灰色空间保持不变。我甚至不知道该如何称呼这个空间--它似乎不是标题的一部分,也不是Picker、表单或区段的一部分。

我发现的关于这个问题的唯一参考是第57851878条,它建议将视图放在标题本身中。这不起作用,而且无论如何都是个坏主意。

图像中的空格是红色,主题是:

https://i.stack.imgur.com/4rZpx.png

这是代码:

struct ContentView: View {

    @State private var thing: String = ""
    @State private var enableSaveButton = false
    @State private var selection = 0
    @State private var selection2 = 0

    var things = ["books","desks","chairs","lamps","couches","shelves"]

    var body: some View {
        NavigationView {
            //ScrollView{
            VStack {
                Group {//first group
                    VStack {
                        Text("Text at the Top")
                        TextField("enter something here", text: $thing)
                            .frame(width:350, height: 50)
                            .clipShape(RoundedRectangle(cornerRadius: 12))
                            .overlay(RoundedRectangle(cornerRadius: 12)
                            .stroke(Color.gray, lineWidth: 2))
                            .padding(.leading, 5)
                            .padding(.bottom, 20)

                        Section(header: HStack {
                            Text("This is the Header")
                                .font(.headline)
                                .foregroundColor(.blue)
                                .padding(.bottom, 0)
                                .padding(.leading, 30)
                            Spacer()
                        }
                        .background(Color.white)
                        .listRowInsets(EdgeInsets(
                            top: 0,
                            leading: 0,
                            bottom: 0,
                            trailing: 0))
                        ) {

                            Form {
                                Text("This is the Chosen Thing: \(self.things[selection2])")

                                Picker(selection: self.$selection2, label: Text("Choose Thing").foregroundColor(.blue)) {
                                    ForEach(0 ..< things.count) {
                                        Text(self.things[$0])
                                    }
                                }//picker
                            }//form
                                .frame(width:350, height: 115)
                                .padding(.top, 0)
                        }//first picker section
                    }//vstack
                }//first group

                Spacer()

                Group {//second Group
                    Text("Enable and Disable this button")
                    Button(action: {
                        print("whatever")
                    } ) {
                        ZStack {
                            RoundedRectangle(cornerRadius: 20)
                                .fill(Color.yellow)
                                .frame(width: 100, height: 40)
                            Text("Save").font(.headline)
                        }
                    }
                    .shadow(radius: 12, x: 10, y: 10)
                    //.disabled(!enableSaveButton)
                }//second Group
            }//outer VStack
                //}//Scrollview
                .navigationBarTitle("Things")
        }//nav view
    }//body
}

Any guidance would be appreciated.  Xcode 11.2.1 (11B500)

展开
收起
游客5akardh5cojhg 2019-12-11 22:28:41 690 0
1 条回答
写回答
取消 提交回答
  • 你可以移除upper和lower空间Form通过添加以下代码

    struct ContentView: View {
    
        init() {
             UITableView.appearance().tableHeaderView = UIView(frame: CGRect(x: 0, y: 0, width: 0, height: Double.leastNonzeroMagnitude))
             UITableView.appearance().tableFooterView = UIView(frame: CGRect(x: 0, y: 0, width: 0, height: Double.leastNonzeroMagnitude))
        }
    
      //your code .....
    }
    
    2019-12-11 22:29:11
    赞同 展开评论 打赏
问答分类:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载