ios 7-无需更改语言即可将视图布局强制转换为RTL [英] ios 7 - Force view layout flip to RTL without language change

查看:58
本文介绍了ios 7-无需更改语言即可将视图布局强制转换为RTL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序中有一些视图需要能够同时显示LTR和RTL内容(尽管不能同时显示),并且它必须与一般的应用程序布局方向无关.意思是说,我希望能够在不更改系统语言的情况下,即时告诉特定视图将其布局从LTR翻转为RTL,反之亦然.

我对此进行了相当多的研究,但除了以下两个解决方案外,似乎不可能:
.

I have a few views in my app that need to be able to display both LTR and RTL content (not at the same time though), and it has to be unrelated to the general app layout direction. Meaning, I want to be able to tell a specific view to flip its layout from LTR to RTL and vice versa, on the fly, without changing the system language.

I researched this quite a bit, but aside of the following two solutions, it seems like it's not possible:
Flip layout on iPhone for RTL languages
How to force "Respect Language Direction" from RTL to LTR and vice versa

The first solution is not relevant in my case. The second solution gave me the idea to just keep two versions for every view, one for RTL and one for LTR, and use the relevant one on the fly. That means I'm going to have to update twice the views on every UI update though.

A third solution, of course, is to manually change the view's layout, programatically, according to the relevant direction, before showing the view.

So, are those my only options?

P.S: I'm talking about the layout only, not the localisation strings, as i'm handling the proper text myself.

解决方案

I found it's quite easy to implement that, just:

func setRTL(_ sender: UIButton) {
    UIView.appearance().semanticContentAttribute = .forceRightToLeft
    UINavigationBar.appearance().semanticContentAttribute = .forceRightToLeft
    if let vc = storyboard?.instantiateViewController(withIdentifier: "root") {
        UIApplication.shared.keyWindow?.rootViewController = vc
    }
}

Preview:

And check out the full DEMO.

这篇关于ios 7-无需更改语言即可将视图布局强制转换为RTL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆