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

如何以编程方式将iOS应用程序的布局从LTR更改为RTL,反之亦然?,ios,localization,right-to-left,Ios,Localization,Right To Left,因此,我尝试通过编程将应用程序语言从English更改为RTL语言。文本已本地化,但布局仍然是LTR,直到我重新启动RTL生效的应用程序 有没有一种方法可以在不重新启动应用程序的情况下实现这一点?在iOS应用程序中,您可以通过调用userInterfaceLayoutDirectionForSemanticContentAttribute:方法来获取UIView实例的布局方向。例如: if ([UIView userInterfaceLayoutDirectionForSemanticConte

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


有没有一种方法可以在不重新启动应用程序的情况下实现这一点?

在iOS应用程序中,您可以通过调用userInterfaceLayoutDirectionForSemanticContentAttribute:方法来获取UIView实例的布局方向。例如:

if ([UIView userInterfaceLayoutDirectionForSemanticContentAttribute:view.semanticContentAttribute] == UIUserInterfaceLayoutDirectionRightToLeft) {

}
Apple提供了支持从右向左语言的方法,链接如下:

最后,我在运行时使用
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];
    }
}