Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
404 views
in Technique[技术] by (71.8m points)

swift - How to divide HStack into two equal parts and place image in the middle of each part?

I want to conditionally divide HStack into two equal parts and place a circle in the middle of each part, how can this be done?

struct ContentView: View {
    
    var body: some View {
        Form{
            HStack{
                Image(systemName: "circle.fill")
                Image(systemName: "circle.fill")
            }
        }
    }
}

As it is

That is how it should be

question from:https://stackoverflow.com/questions/65873595/how-to-divide-hstack-into-two-equal-parts-and-place-image-in-the-middle-of-each

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

We can use two transparent colors that fill equally available space and put images into overlay, which uses centred default layout, like

enter image description here

var body: some View {
    Form{
        HStack{
            Color.clear.overlay(Image(systemName: "circle.fill"))
            Color.clear.overlay(Image(systemName: "circle.fill"))
        }
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...