I am trying to add a confirm button to the top-right of a text editor screen by using the SwiftUI toolbar.
I realised that the text in the button cannot be centered as long as the ToolbarItem placement is configured to set the item to the top-right of the screen.
Is this a bug?
Here is a sample code:
import SwiftUI
struct TestView: View {
var body: some View {
NavigationView {
ZStack {
Color(.black)
.ignoresSafeArea()
Text("Hello World")
.foregroundColor(.white)
.toolbarRole(.editor)
.toolbar {
ToolbarItem(placement: .confirmationAction) {
Button(action: {print("confirm.")}) {
Text("confirm")
}
.background(.yellow)
.foregroundColor(.white)
.cornerRadius(20.0)
}
}
}
}
}
}
struct TestView_Previews: PreviewProvider {
static var previews: some View {
TestView()
}
}
code-block
Thanks for feedback and congrats for finding a solution. You should file a bug report, to understand.
Reason could be that in the toolbar, the button is itself in a container and alignment is relative to this outside container ?
Some explanations here: https://sarunw.com/posts/how-to-align-text-in-swiftui/