简体   繁体   English

SwiftUI 无法删除选择器上方的空间 - 表单版本

[英]SwiftUI Can't Remove Space Above Picker - Form Version

SwiftUI Can't Remove Space Above Picker - Form Version SwiftUI 无法删除选择器上方的空间 - 表单版本

I'm struggling mightily with the formatting of pickers in SwiftUI.我在 SwiftUI 中为选择器的格式苦苦挣扎。 I built a simple picker with a few other views in a single view app.我在单个视图应用程序中构建了一个带有其他一些视图的简单选择器。 There is a space between the Header and the picker that I can't remove.标题和选择器之间有一个我无法删除的空间。 I'd settle for setting the gray color to white.我会满足于将灰色设置为白色。 If I change the frame of the picker it just scrunches the picker - the gray space remains untouched.如果我更改选择器的框架,它只会使选择器收缩 - 灰色空间保持不变。 I'm not sure what to even call that space - it does not appear to be part of the Header, nor the Picker, nor the Form nor the Section.我什至不知道如何称呼那个空间 - 它似乎不是标题的一部分,也不是选择器,也不是表格或部分。

The only reference to this issue that I found was article 57851878 which suggests putting the view in the header itself.我发现的对这个问题的唯一参考是文章 57851878,它建议将视图放在标题本身中。 That does not work and would be a really bad idea anyway.这是行不通的,无论如何都是一个非常糟糕的主意。

The space in the image outlined is red is the subject:图中勾勒出的红色空间为主体:

在此处输入图片说明

And this is the code:这是代码:

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)

You can remove upper and lower space of Form by adding below code您可以去除upperlower的空间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 .....
}

Another possibility is to set the background color for the table view and table view cell to clear in init()另一种可能性是在 init() 中设置 table view 和 table view cell 的背景颜色以清除

Init() {
    UiTableView.appearance().backgroundColor = .clear
    UITableViewCell.appearance().backgroundcolor = .clear
}

This will not get rid of the space, but it will make the underlying table transparent to the window background color这不会摆脱空间,但会使底层表格对窗口背景颜色透明

it looks like it is a section header of the form...看起来它是表单的节标题......

if you change your code like this you will see, you have a section header text.如果您像这样更改代码,您会看到,您有一个部分标题文本。 Why SwiftUI does this by itself ...i don't know....unfortunately it even does not disappear with frame set to height 0....为什么 SwiftUI 自己这样做......我不知道......不幸的是,它甚至不会在框架设置为高度 0 的情况下消失......

Form {
    Section(header: Text("General Settings")){
    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)

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

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