skip to Main Content

I have a SwiftUI Image view that displays only an SF symbol:

GeometryReader { g in
    Image(systemName: "a.square")
        .font(.system(size: g.size.width))
    }
}

It renders like this:

Preview of Image view

I tried modifying the view with the .multilineTextAlignment(.center) modifier to center it, and that didn’t do anything.

2

Answers


  1. You can try adding this command line for it:

    .position(x: g.size.width / 2, y: g.size.width / 2)
    

    The code you wrote looks like this:

    GeometryReader { g in
       Image(systemName: "a.square")
           .font(.system(size: g.size.width))
           .position(x: g.size.width / 2, y: g.size.width / 2)
    }
    

    }

    enter image description here

    Login or Signup to reply.
  2. Why not the declarative way?

    VStack {
        Image(systemName: "a.square")
            .resizable()
            .aspectRatio(1.0, contentMode: .fit)
            .padding()
        Spacer()
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search