I’m trying to port an Android application to iOS. I would like for the textfield to be aligned in the middle. How do I achieve this? Here is the code so far:
var body: some View
{
GeometryReader
{
metrics in
VStack (alignment: .center)
{
Text("Lazuli Social")
.font(.largeTitle)
.foregroundColor(Color(red: 0.02, green: 0.325, blue: 1.0))
TextField("Username or email", text: $user)
.textFieldStyle(.roundedBorder)
.frame(width: metrics.size.width*0.80)
.background(Color.orange)
}
}
}
2
Answers
You can use the
alignment
of theframe
of theVStack
. You’ll also want to set themaxWidth
toinfinity
so it expands.In general, you may want to try to avoid using
GeometryReader
except when you absolutely need a size. For example, here, you may want to trypadding
instead (albeit it gives a slightly different result — it’ll no longer be 80% of the width all the time).Maybe try:
.multilineTextAlignment(.center)