swiftui - 编辑 : How we can alignment Image with Ima

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

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)
}

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

相关文章:

ruby-on-rails - 使用 Cloudinary 和 Active Storage 时如何

java - 无效的正则表达式 : Dangling meta character "*"

python - 无法用pip安装pycuda

unity3d - 如何将 Input.GetAxis ("Mouse X") 或 Input.Ge

terraform - 如何定义 "azurerm_resource_group_template_

reactjs - 如何触发事件 React 测试库

github - 使用 SonarQube(社区版)装饰 GitHub 中的拉取请求

android - 如何解决 android.content.res.Resources$NotFo

json - 使用 Mule 中的 Dataweave 将定界文件转换为 JSON 格式

C - 如何通过函数增加指向字符串的指针?