无法在Swiftui中对齐多个控件

发布于 2025-01-28 04:43:54 字数 2931 浏览 2 评论 0原文

“上周活动”之间的距离是

我正在尝试使用Swiftui进行对齐控件,但不确定为什么在段控制和搜索栏之间以及扫描图标与以下 Swiftui:

import SwiftUI

struct ContentView: View {
    
    var body: some View {
        VStack{
            ContentViewHeader().background(Color.yellow)
            ContentViewSearchBar().background(Color.cyan)
            ContentViewFilter().background(Color.green)
            ContentViewTableView().background(Color.orange)
        }
    }
}

struct ContentViewHeader: View {
    @State private var favoriteColor = 0
    
  var body: some View {
      VStack {
          Text("Colors & Content ").font(.system(size: 21, weight: .bold, design: .default)).multilineTextAlignment(.leading).padding()
          Spacer()
                  .frame(height: 30)
          Picker("What is your favorite color?", selection: $favoriteColor) {
                          Text("Red").tag(0)
                          Text("Green").tag(1)
                          Text("Blue").tag(2)
         }.pickerStyle(.segmented)
      }
      .frame(minWidth: 0, maxWidth: .infinity, alignment: .leading)
      .navigationViewStyle(StackNavigationViewStyle())
    }
}


struct ContentViewSearchBar: View {
    @State private var searchText = ""

        var body: some View {
            NavigationView {
                Text("")
                    .searchable(text: $searchText, prompt: "Search")
            }
    }
}

struct ContentViewTableView: View {
    
    var dataTypeList = [
        DataType(name: "Integer", size: "4 bytes", color: .red),
        DataType(name: "Character", size: "1 byte", color: .blue),
        DataType(name: "Float", size: "4 bytes", color: .green),
        DataType(name: "Double", size: "8 bytes", color: .yellow),
        DataType(name: "Integer", size: "4 bytes", color: .red),
        DataType(name: "Character", size: "1 byte", color: .blue),
        DataType(name: "Float", size: "4 bytes", color: .green),
        DataType(name: "Double", size: "8 bytes", color: .yellow),
      ]
    
  var body: some View {
      List(dataTypeList, id: \.name) { dataType in
            HStack {
              Text(dataType.name)
              Text(dataType.size).foregroundColor(dataType.color)
            }
     }
  }
}

struct DataType {
  let name: String
  let size: String
  let color: Color
}

struct ContentViewFilter: View {
  var body: some View {
      HStack {
          Text("           Last week activities                           ").font(.system(size: 10, weight: .bold, design: .default)).padding()
          
          Button(action: {
              // do something here
          }, label: {
                   Image("scan")
          }).frame(width: 40, height: 40, alignment: .center)
      }
  }


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

不确定我需要在SwiftUI代码中进行哪些校正,以便删除段控制和搜索栏之间的差距/距离,甚至对于扫描图标也被删除。它应该类似于故事板图像的图像。

I'm trying to align controls as shown in "Storyboard" pic using the SwiftUI but not sure why still there is distance between segment control and search-Bar, and also between scan icon and the "Last week activities"

Below is the code of SwiftUI :

import SwiftUI

struct ContentView: View {
    
    var body: some View {
        VStack{
            ContentViewHeader().background(Color.yellow)
            ContentViewSearchBar().background(Color.cyan)
            ContentViewFilter().background(Color.green)
            ContentViewTableView().background(Color.orange)
        }
    }
}

struct ContentViewHeader: View {
    @State private var favoriteColor = 0
    
  var body: some View {
      VStack {
          Text("Colors & Content ").font(.system(size: 21, weight: .bold, design: .default)).multilineTextAlignment(.leading).padding()
          Spacer()
                  .frame(height: 30)
          Picker("What is your favorite color?", selection: $favoriteColor) {
                          Text("Red").tag(0)
                          Text("Green").tag(1)
                          Text("Blue").tag(2)
         }.pickerStyle(.segmented)
      }
      .frame(minWidth: 0, maxWidth: .infinity, alignment: .leading)
      .navigationViewStyle(StackNavigationViewStyle())
    }
}


struct ContentViewSearchBar: View {
    @State private var searchText = ""

        var body: some View {
            NavigationView {
                Text("")
                    .searchable(text: $searchText, prompt: "Search")
            }
    }
}

struct ContentViewTableView: View {
    
    var dataTypeList = [
        DataType(name: "Integer", size: "4 bytes", color: .red),
        DataType(name: "Character", size: "1 byte", color: .blue),
        DataType(name: "Float", size: "4 bytes", color: .green),
        DataType(name: "Double", size: "8 bytes", color: .yellow),
        DataType(name: "Integer", size: "4 bytes", color: .red),
        DataType(name: "Character", size: "1 byte", color: .blue),
        DataType(name: "Float", size: "4 bytes", color: .green),
        DataType(name: "Double", size: "8 bytes", color: .yellow),
      ]
    
  var body: some View {
      List(dataTypeList, id: \.name) { dataType in
            HStack {
              Text(dataType.name)
              Text(dataType.size).foregroundColor(dataType.color)
            }
     }
  }
}

struct DataType {
  let name: String
  let size: String
  let color: Color
}

struct ContentViewFilter: View {
  var body: some View {
      HStack {
          Text("           Last week activities                           ").font(.system(size: 10, weight: .bold, design: .default)).padding()
          
          Button(action: {
              // do something here
          }, label: {
                   Image("scan")
          }).frame(width: 40, height: 40, alignment: .center)
      }
  }


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

Not sure what correction I need to do in SwiftUI code to so that gaps/distance between segment control and search-bar are removed and even for scan icon also. It should be similar to that of storyboard image.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文