SwiftUI:如何在列表中居中放置矩形?
如何使列表中的矩形居中?我已经在我的代码中尝试了一些东西,但是没有效果。这与列表的默认行为有关吗 这是我的密码:SwiftUI:如何在列表中居中放置矩形?,swift,swiftui,Swift,Swiftui,如何使列表中的矩形居中?我已经在我的代码中尝试了一些东西,但是没有效果。这与列表的默认行为有关吗 这是我的密码: struct ContentView: View { var sectiesList = [ Secties(name: "Algemeen", color: Color.overigPink), Secties(name: "Natuurkunde", color: Color.natuurkundeBlue), S
struct ContentView: View {
var sectiesList = [
Secties(name: "Algemeen", color: Color.overigPink),
Secties(name: "Natuurkunde", color: Color.natuurkundeBlue),
Secties(name: "Wiskunde", color: Color.wiskundeBrown),
Secties(name: "Scheikunde", color: Color.scheikundeRed),
Secties(name: "Biologie", color: Color.biologieGreen)
]
var body: some View {
NavigationView {
ZStack {
Color.offWhite
List(sectiesList, id: \.name) { secties in
ZStack(alignment: .center) {
RoundedRectangle(cornerRadius: 25)
.fill(secties.color)
.frame(width: UIScreen.screenWidth * 0.85, height: UIScreen.screenHeight * 0.13, alignment: .center)
.shadow(color: Color.black.opacity(0.2), radius: 10, x: 10, y: 10)
.shadow(color: Color.white.opacity(0.7), radius: 10, x: -5, y: -5)
Text(secties.name)
.font(.largeTitle)
.fontWeight(.semibold)
.foregroundColor(Color.white)
.multilineTextAlignment(.center)
}
}
}.navigationBarTitle(Text("Binas"))
}
}
init() {
UITableView.appearance().separatorStyle = .none
UITableViewCell.appearance().backgroundColor = UIColor(red: 225 / 255, green: 225 / 255, blue: 235 / 255, alpha: 1)
UITableView.appearance().backgroundColor = UIColor(red: 225 / 255, green: 225 / 255, blue: 235 / 255, alpha: 1)
}
}为ZStack提供行中的最大可用空间,如
ZStack(alignment: .center) {
RoundedRectangle(cornerRadius: 25)
.fill(secties.color)
.frame(width: UIScreen.screenWidth * 0.85, height: UIScreen.screenHeight * 0.13, alignment: .center)
.shadow(color: Color.black.opacity(0.2), radius: 10, x: 10, y: 10)
.shadow(color: Color.white.opacity(0.7), radius: 10, x: -5, y: -5)
Text(secties.name)
.font(.largeTitle)
.fontWeight(.semibold)
.foregroundColor(Color.white)
.multilineTextAlignment(.center)
}
.frame(maxWidth: .infinity) // << here !!
ZStack(对齐:。中心){
圆角转角(拐角半径:25)
.填充(截面.颜色)
.frame(宽度:UIScreen.screenWidth*0.85,高度:UIScreen.screenHeight*0.13,对齐:。中间)
阴影(颜色:颜色。黑色。不透明度(0.2),半径:10,x:10,y:10)
阴影(颜色:彩色。白色。不透明度(0.7),半径:10,x:-5,y:-5)
文本(secties.name)
.font(.largeTitle)
.fontWeight(.半加粗)
.foregroundColor(颜色.白色)
.multilitextalignment(.center)
}
.frame(最大宽度:。无穷大)//