ios Swift UICollectionViewCompositionalLayout Mirrored UI on switching UI Layout direction

ios Swift UICollectionViewCompositionalLayout Mirrored UI on switching UI Layout direction

每当我使用以下代码在运行时切换语言时:

//set apple language to force localization use the intended language 
let userdef = UserDefaults.standard
userdef.set(["ar" /*or "en" to switch to english*/], forKey: "AppleLanguages")
userdef.synchronize()
//force all views to use propery layout direction
UIView.appearance().semanticContentAttribute = forceRightToLeft /*or .forceLeftToRight*/
//reload root view controller
(UIApplication.shared.delegate as? AppDelegate)?.window?.rootViewController = UIStoryboard(name: "Main", bundle: .main).instantiateInitialViewController()

任何使用组合布局的集合视图都会被镜像,不是布局翻转而是内容被镜像。

终于想通了,一定要走老路:

1- 对于集合视图,无论何时创建(在故事板中或通过代码),修复语义以强制 LTR

2- 每当语言需要 RTL 时,将 scaleX = -1、scaleY = 1 的变换应用到 CollectioView 本身。

3- 对于每个视图单元格,应用另一个变换,scaleX = -1,scaleY = 1

Note 对于某些布局(主要是具有动态项目宽度的水平滚动),直接将转换分配给集合视图单元格将不起作用,并且会离开 CollectionView 单元格mirrored 通过集合视图变换。

3.1- 因此需要将转换应用于集合视图单元格的顶级子视图。

为了最小化代码,应该将集合视图单元格的所有子视图包装到一个填充集合视图单元格的视图中,并对其应用变换。