gpt4 book ai didi

swiftui - 编辑 : How we can alignment Image with Images and Text withTexts in SwiftUI, 多重对齐?

转载 作者:行者123 更新时间:2023-12-05 08:49:15 25 4
gpt4 key购买 nike

我有一个 VStack,它有一些 HStack,正如您在我的代码中看到的那样,在我的每个 Hstack 中都有一个图像和文本,在运行我的代码之后,所有代码的 Alignmet 都很丑陋,我希望图像对齐中心在一起并且文本对齐领先。我该如何解决这个问题?我可以使所有图像 .center 对齐,也可以使所有文本 .leading 对齐。但我不能让两者同时发生。

enter image description here

enter image description here enter image description here

struct CustomAlignment: AlignmentID
{
static func defaultValue(in context: ViewDimensions) -> CGFloat
{
return context[HorizontalAlignment.center]
}
}


struct CustomAlignment2: AlignmentID
{
static func defaultValue(in context: ViewDimensions) -> CGFloat
{
return context[HorizontalAlignment.leading]
}
}



extension HorizontalAlignment
{
static let custom: HorizontalAlignment = HorizontalAlignment(CustomAlignment.self)
static let custom2: HorizontalAlignment = HorizontalAlignment(CustomAlignment2.self)
}




import SwiftUI

struct ContentView: View {
var body: some View {





VStack(alignment: .custom)
{


HStack()
{
Image(systemName: "folder")
.alignmentGuide(.custom) { $0[HorizontalAlignment.center] }
Text("Some texts here.")
.alignmentGuide(.custom2) { $0[HorizontalAlignment.leading] }
Spacer()
}



HStack()
{
Image(systemName: "music.note")
.alignmentGuide(.custom) { $0[HorizontalAlignment.center] }
Text("Some texts here.")
.alignmentGuide(.custom2) { $0[HorizontalAlignment.leading] }
Spacer()
}



HStack()
{
Image(systemName: "person.fill.questionmark")
.alignmentGuide(.custom) { $0[HorizontalAlignment.center] }
Text("Some texts here.")
.alignmentGuide(.custom2) { $0[HorizontalAlignment.leading] }
Spacer()
}



}
.padding()



Spacer()



}

最佳答案

如果您想要精确控制,请使用自定义对齐指南。

关于您对使用固定框架的评论,here是一篇解释框架如何在 SwiftUI 中工作的文章。基本上,在这种情况下,框架修改器只是在 SF 周围添加一个固定大小的框架,但它不会改变固有大小。

struct ContentView: View {
var body: some View {
VStack(alignment: .sfView) {
SFView(title: "This is some text", image: "folder")
SFView(title: "SwiftUI is cool. Combine is cooler.", image: "snow")
SFView(title: "This is a music note. This has a different length.", image: "music.note")
}
}
}

private struct SFView: View {
let title: String
let image: String
var body: some View {
HStack(spacing: 8) {
Image(systemName: image)
.font(.system(size: 20))
.frame(width: 32, height: 32)
.alignmentGuide(.sfView) { d in d[HorizontalAlignment.center] }
Text(title)
.alignmentGuide(.sfView) { d in d[HorizontalAlignment.leading] }
}
}
}

private extension HorizontalAlignment {
struct SFViewAlignment: AlignmentID {
static func defaultValue(in d: ViewDimensions) -> CGFloat {
d[HorizontalAlignment.leading]
}
}
static let sfView = HorizontalAlignment(SFViewAlignment.self)
}

enter image description here

关于swiftui - 编辑 : How we can alignment Image with Images and Text withTexts in SwiftUI, 多重对齐?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64278774/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com