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

时间:2017-05-24 11:36:07

标签: ios localization right-to-left

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

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

2 个答案:

答案 0 :(得分:0)

我最终使用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];
    }
}

答案 1 :(得分:-1)

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

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

}

Apple提供了支持从右到左语言的方法,这里是链接: https://developer.apple.com/library/content/documentation/MacOSX/Conceptual/BPInternational/SupportingRight-To-LeftLanguages/SupportingRight-To-LeftLanguages.html