Search code examples
ioslocalizationright-to-left

How to change the layout of an iOS app programmatically from LTR to RTL and vice versa?


So, I tried changing the app language programmatically from English to an RTL language. The text is localized but the layout is still LTR until I restart the app that RTL takes effect.

Is there a way to make this happen without restarting the app?


Solution

  • I ended up forcing RTL in runtime using the setSemanticContentAttribute method like bellow:

    if ([NSLocale characterDirectionForLanguage:languageCode] == NSLocaleLanguageDirectionRightToLeft) {
        if ([[[UIView alloc] init] respondsToSelector:@selector(setSemanticContentAttribute:)]) {
            [[UIView appearance] setSemanticContentAttribute:
             UISemanticContentAttributeForceRightToLeft];
        }
    }
    else {
        if ([[[UIView alloc] init] respondsToSelector:@selector(setSemanticContentAttribute:)]) {
            [[UIView appearance] setSemanticContentAttribute:UISemanticContentAttributeForceLeftToRight];
        }
    }