无法在Swiftui中对齐多个控件
“上周活动”之间的距离是
我正在尝试使用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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论