How to change .navigationBarTitle alignment to trailing for iOS in SwiftUI (or, make it Right To Left)

Viewed 1159

I've modified the title font, color and background of navigationBar using UINavigationBar.appearance().titleTextAttributes and UINavigationBar.appearance().backgroundColor.

However I can't find any attribute to set the navigationBarTitle alignment to right (trailing).

Additionally, I've even tried .navigationBarTitle(Text("Title").multilineTextAlignment(.leading)) modifier, but SwiftUI does not accept it. Any solution will be appreciated.

1 Answers

It is possible to change any view right-to-left by giving them below modifiers:

.environment(\.layoutDirection, .rightToLeft)
.flipsForRightToLeftLayoutDirection(true)

And it can be used for the navigation bar as well:

NavigationView {

  VStack {

//Your views ...

    .navigationBarTitle(Text("Title"))
  }

}
.environment(\.layoutDirection, .rightToLeft)
.flipsForRightToLeftLayoutDirection(true)
Related