简体   繁体   中英

Reliable iOS image flipping for RTL languages

I'm trying to get image flipping to work reliably, but i'm seeing some issues. I manage my images in an Xcode asset catalog, and the ones that need to be flipped in RTL languages have been configured with the 'Direction' property set to 'Left to right, Mirrors'. In some cases I have 2 separate assets, since flipping was not enough. Those are configured with the 'Direction' property set to 'Both' and two separate image assets are provided. All images are PDF assets with 'Preserve vector data' enabled.

This all worked great as long as I test with iOS 11 and Xcode 9.2. The problems start when I test on iOS 9. Images that are configured for RTL don't show up when I launch the app, even when I'm not running in a RTL language.

Since Xcode 9.3, I seem to have a new problem. The asset catalogs get updated automatically; the RTL images are reconfigured to direction 'Both' and changes are made to the json files in the catalog. Selecting 'Left to right, Mirrors' does not work anymore.

Is there anyone who has figured out how to get this to work reliably? Or am I just looking at the latest bugs in Xcode and some old issues with iOS9?

[edit] After further analysis of the IPA file that is generated, it seems that the Assets.car file does not contain .png files for the RTL images. It seems that they are not generated (since the source files are PDF), so that would explain the missing images in iOS 9 (which does not use PDF images).

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];

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"];
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM