Flip UIImageViews for Right-Handed Languages

iOS automatically flips the entire ViewController when using RTL, such as Arabic, and does a great job with most layouts, especially text. The default behavior is to flip the layout, but leave the UIImageViews the same orientation (since you don't want to do inverse images at all).

Is it possible to indicate that some images should be turned upside down (for example, arrows) when RTL is installed on the phone?

+6
ios internationalization
source share
5 answers

iOS 9 includes an imageFlippedForRightToLeftLayoutDirection method that you can use that automatically flips the image into a UIImageView when localizing RTL.

+9
source share

You need to manually flip UIImages into the UIImageView that you want when RTL is installed on the phone. This can be easily achieved with this code:

 UIImage* defaultImage = [UIImage imageNamed:@"default.png"]; UIImage* flipImage = [UIImage imageWithCGImage:sourceImage.CGImage scale:1.0 orientation: UIImageOrientationUpMirrored]; myImageview.image = flipImage; 
+3
source share

As a result, I used localized images for the forward and reverse arrows. This had the advantage that you did not have to add code to every place where the image was used, and it made it possible to clear arrows if there are gradients that do not work well.

0
source share

While we expect iOS 9 support to improve right to left, you can subclass UIImageView and override setImage to mirror inside the images, as @ nikos-m offers and calls super.image = flipImage .

That way, you can easily set all kinds of images you want to flip using custom classes in Interface Builder instead of adding IBOutlet s.

0
source share

We can use imageFlippedForRightToLeftLayoutDirection , which returns the reversed image if the current language is RTL (from right to left). i.e

Objective-c

 UIImage * flippedImage = [[UIImage imageNamed:@"imageName"] imageFlippedForRightToLeftLayoutDirection]; 

Swift 3

 let flippedImage = UIImage(named: "imageName")?.imageFlippedForRightToLeftLayoutDirection() 

Source: Apple Docs

0
source share

All Articles