如何解决如何将Hstack内部的视图与相对侧对齐?
我一直在尝试制作SwiftUI视图,但是在做一些可以很容易地用约束来完成的事情时遇到了麻烦。 我的观点应该是这样
看起来像这样
如何在没有约束的情况下使它们与侧面对齐? 这是我针对此视图的代码
private var toolsBottomView: some View {
ScrollView(.vertical,showsIndicators: false) {
VStack(spacing: 20) {
ForEach(0..<AppTools.allCases.count) { index in
ZStack(alignment: .leading) {
RoundedRectangle(cornerRadius: 16)
.frame(height: 116)
.foregroundColor(Color(#colorLiteral(red: 0.1294117647,green: 0.137254902,blue: 0.1882352941,alpha: 1)))
HStack(alignment: .center,spacing: 0) {
Spacer().frame(width: 20)
VStack(alignment: .leading) {
Image("\(AppTools.allCases[index])").resizable().frame(width: 27,height: 27)
Text(AppTools.allCases[index].rawValue)
.font(.custom("AvenirNextCyr-Demi",size: 24))
Text(AppTools.allCases[index].subtitles)
.font(.custom("AvenirNextCyr-Demi",size: 12))
.foregroundColor(Color(#colorLiteral(red: 0.4823529412,green: 0.4980392157,blue: 0.6196078431,alpha: 1)))
}.foregroundColor(.white)
Button("+",action: {
self.currentSelectedTool = AppTools.allCases[index]
self.showToolView.toggle()
})
.font(.custom("AvenirNextCyr-Demi",size: 20))
.frame(width: 48,height: 48)
.background(Color(#colorLiteral(red: 1,green: 0.6745098039,alpha: 1)).cornerRadius(24))
.foregroundColor(Color(#colorLiteral(red: 0.1294117647,alpha: 1)))
}
.frame(minWidth: 0,maxWidth: .infinity)
}
}
Spacer()
}
}.offset(y: 15).padding()
}
解决方法
将Spacer
放在要保留的元素之间,例如
HStack(alignment: .center,spacing: 0) {
VStack(alignment: .leading) {
Image("\(AppTools.allCases[index])").resizable().frame(width: 27,height: 27)
Text(AppTools.allCases[index].rawValue)
.font(.custom("AvenirNextCyr-Demi",size: 24))
Text(AppTools.allCases[index].subtitles)
.font(.custom("AvenirNextCyr-Demi",size: 12))
.foregroundColor(Color(#colorLiteral(red: 0.4823529412,green: 0.4980392157,blue: 0.6196078431,alpha: 1)))
}.foregroundColor(.white)
.padding(.leading)
Spacer() // << here !!
Button("+",action: {
self.currentSelectedTool = AppTools.allCases[index]
self.showToolView.toggle()
})
.font(.custom("AvenirNextCyr-Demi",size: 20))
.frame(width: 48,height: 48)
.background(Color(#colorLiteral(red: 1,green: 0.6745098039,blue: 0.1882352941,alpha: 1)).cornerRadius(24))
.foregroundColor(Color(#colorLiteral(red: 0.1294117647,green: 0.137254902,alpha: 1)))
.padding(.trailing)
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。