I know SwiftUI changes the direction of the application, based on the system language of the device. but is there a way to ignore the system language and force the app to be always RTL or LTR?
You may one to make the app default language to some Right To Left like Persian
in info.plist
or with other methods:
If you need to force a view, Just apply this modifier on any view you like:
.environment(\.layoutDirection, .rightToLeft)
You can apply to the ContentView()
in SceneDelegate
to make it available for all subviews.
You can either set layout direction in the Scene Delegate:
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
let contentView = ContentView().environment(\.layoutDirection, .rightToLeft)
if let windowScene = scene as? UIWindowScene {
let window = UIWindow(windowScene: windowScene)
window.rootViewController = UIHostingController(rootView: contentView)
self.window = window
window.makeKeyAndVisible()
}
}
or manually disable change of direction on some views using this modifier:
.flipsForRightToLeftLayoutDirection(true)
I tried everything to force SwiftUI to be RTL Arabic, Like...
import SwiftUI
@main
struct MyApp: App {
init() {
UserDefaults.standard.set(["ar"], forKey: "AppleLanguages")
}
var body: some Scene {
WindowGroup {
ContentView()
.environment(\.locale, Locale(identifier: "ar"))
.environment(\.layoutDirection, .rightToLeft)
}
}
}
But this way cause a lot of bugs if you use NavigationView The best solution is...
import SwiftUI
@main
struct MyApp: App {
init() {
UserDefaults.standard.set(["ar"], forKey: "AppleLanguages")
}
var body: some Scene {
WindowGroup {
ContentView()
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With