用于 RTL 语言的可靠 iOS 图像翻转

Bar*_*tra 2 xcode right-to-left ios

我试图让图像翻转可靠地工作,但我看到了一些问题。我在 Xcode 资产目录中管理我的图像,需要在 RTL 语言中翻转的图像已配置为“方向”属性设置为“从左到右,镜像”。在某些情况下,我有 2 个单独的资产,因为翻转是不够的。这些配置为“方向”属性设置为“两者”,并提供了两个单独的图像资产。所有图像都是启用了“保留矢量数据”的 PDF 资产。

只要我在 iOS 11 和 Xcode 9.2 上测试,这一切都很好。当我在 iOS 9 上测试时,问题就开始了。当我启动应用程序时,为 RTL 配置的图像不会显示,即使我没有使用 RTL 语言运行。

从 Xcode 9.3 开始,我似乎遇到了一个新问题。资产目录自动更新;RTL 图像被重新配置为“两者”方向,并对目录中的 json 文件进行了更改。选择“从左到右,镜子”不再起作用。

有没有人想出如何让它可靠地工作?还是我只是在查看 Xcode 中的最新错误和 iOS9 的一些旧问题?

[编辑] 进一步分析生成的 IPA 文件后,似乎 Assets.car 文件不包含 RTL 图像的 .png 文件。似乎它们没有生成(因为源文件是 PDF),所以这可以解释 iOS 9 中丢失的图像(不使用 PDF 图像)。

Bar*_*tra 8

I fixed it, but it took someone at the Apple Developer Forum to point out that the asset catalog 'direction' property was not introduced until Xcode 8 / iOS 10.

This means it just doesn't work in iOS 9, and it is probably an Xcode bug that you can even select it when the deployment target is set below iOS 10.

So, don't try to use this feature when you want to be compatible with older iOS devices! You can still get it to work programatically.

If you have an image in for example a UIBarButtonItem, you can make an outlet to this button and run the following in the viewDidLoad:

self.someButton.image = [self.someButton.image imageFlippedForRightToLeftLayoutDirection];
Run Code Online (Sandbox Code Playgroud)

This works because imageFlippedForRightToLeftLayoutDirection is supported by iOS 9. It only flips the image when your app is in RTL mode.

If you need to load a completely different image, you can do that as follows:

if ([UIApplication sharedApplication].userInterfaceLayoutDirection == UIUserInterfaceLayoutDirectionRightToLeft) {
    self.someButton.image = [UIImage imageNamed:@"someRTLImage"];
}
Run Code Online (Sandbox Code Playgroud)