【问题标题】:SwiftUI Can't Remove Space Above Picker - Form VersionSwiftUI 无法删除选取器上方的空间 - 表单版本
【发布时间】:2019-12-11 02:50:07
【问题描述】:

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

我正在为 SwiftUI 中的选择器格式而苦苦挣扎。我建立了一个简单的 选择器与单个视图应用程序中的其他一些视图。 Header 之间有空格 和我无法删除的选择器。我会满足于将灰色设置为白色。如果 我改变了选择器的框架,它只是挤压了选择器 - 灰色空间仍然存在 原封不动。我不知道该叫什么空间——它似乎不是一部分 标头,选择器,表单或部分。

我发现对这个问题的唯一参考是文章 57851878,它建议将 标题本身的视图。那是行不通的,而且无论如何都是个坏主意。

图片中的空白处是红色的就是主题:

这是代码:

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)

【问题讨论】:

    标签: ios xcode swiftui


    【解决方案1】:

    您可以通过添加以下代码删除Formupperlower空格

    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 .....
    }
    

    【讨论】:

      【解决方案2】:

      另一种可能性是在 init() 中设置表格视图和表格视图单元格的背景颜色以清除

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

      这不会消除空间,但会使底层表格对窗口背景颜色透明

      【讨论】:

        【解决方案3】:

        它看起来像是表单的部分标题...

        如果您像这样更改代码,您会看到,您有一个节标题文本。为什么 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)
        

        【讨论】:

        • 我会看看这个 - 但在我看来,这只会将文本放入我想要删除的区域。
        猜你喜欢
        • 1970-01-01
        • 2019-12-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-03-14
        • 2020-06-17
        相关资源
        最近更新 更多