如何以编程方式将 iOS 应用的布局从 LTR 更改为 RTL,反之亦然



因此,我尝试以编程方式将应用程序语言从English更改为RTL语言。文本已本地化,但布局仍LTR,直到我重新启动RTL生效的应用程序。

有没有办法在不重新启动应用程序的情况下实现这一目标?

我最终使用如下所示的setSemanticContentAttribute方法在运行时强制 RTL:

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];
    }
}

最新更新