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

半腔热情 提交于 2020-01-25 08:55:05

问题


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:


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)


来源:https://stackoverflow.com/questions/58391821/how-to-change-navigationbartitle-alignment-to-trailing-for-ios-in-swiftui-or

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!