Position view relative to a another centered view

折月煮酒 提交于 2020-06-17 08:03:45

问题


Hello SwiftUI community,

I'm trying to do something that's probably easy to do but I'm stuck on it since hours. In a list, I'd like to have items composed of:

  • A text centered horizontaly (a number)
  • A text on the left of the number
  • A text on the right of the number

I've tried many things (alignments, aligmentguides, GeometryReader....) but didn't find the way to achieve the result above. Does anyone has an example to help me?

Thanks 🙏🏻


回答1:


Here is possible solution. Tested with Xcode 11.4 / iOS 13.4

A view for List row

struct DemoCenteredNumberView: View {
    var value: Int
    var body: some View {
        HStack {
            Spacer().overlay(
                Text("Text on left side")
                    .frame(maxWidth: .infinity, alignment: .trailing)
            )
            Text("\(value)").padding()
            Spacer().overlay(
                Text("Text on right")
                    .frame(maxWidth: .infinity, alignment: .leading)
            )
        }
    }
}


来源:https://stackoverflow.com/questions/62386021/position-view-relative-to-a-another-centered-view

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!