Flip UIImageViews for Right to Left Languages
Asked Answered
M

7

26

iOS automatically flips the entire ViewController when using a RTL language like Arabic and does a great job with most of the layout, especially text. The default behavior is to flip the layout but leave UIImageViews the same orientation (since you generally don't want to reverse images).

Is there a way to specify that some images should be flipped (such as arrows) when the phone is set to a RTL language?

Margiemargin answered 3/12, 2013 at 14:58 Comment(0)
F
26

iOS 9 includes the imageFlippedForRightToLeftLayoutDirection method that you can use, that automatically flips the image in a UIImageView when in an RTL localization.

Forecast answered 14/7, 2015 at 14:38 Comment(1)
This is the work only for using different images for specific language. for the flips the image developer can use the iOS 9.0 methods which is introduce for the Right To Left view layout is imageFlippedForRightToLeftLayoutDirection, this methods will flip the image in horizontal direction only.Broeder
H
17

The best solution I found to date is marking the image in the assets file as mirror. enter image description here

Heaviside answered 28/6, 2020 at 23:47 Comment(3)
But, when the image is loading for the first time it is not changing as mirror, later it is working properly.. Why?Tacky
is this in sim? I've noticed this when you change the language and the app is loaded. But if you have the correct language and then run the app it is ok.Heaviside
solved. after uninstall and install, it's working properlyTacky
B
10

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

Objective-c

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

Swift 3

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

Source: Apple Docs

Brittan answered 4/11, 2017 at 18:36 Comment(0)
B
2

You have to manually flip the UIImages in the UIImageViews you want when the phone is set to a RTL language. 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;
Banting answered 3/12, 2013 at 15:13 Comment(1)
That is one possible way, I was hoping there was a way to do it without having to add IBOutlets for each image I wanted to change.Margiemargin
M
1

I ended up using localized images for the forward and back arrows. This had the advantage of not having to add code each place the image was used and gives the opportunity to clean up the arrows if there are gradients that don't work well flipped.

Margiemargin answered 7/12, 2013 at 19:20 Comment(0)
G
0

While we wait for iOS 9 improved right to left support you could create a UIImageView subclass and override setImage to mirror inside the images as @nikos-m suggests and calling super.image = flipImage.

That way you can easily set all the images views you want to flip using custom classes in Interface Builder instead of having to add IBOutlets.

Galactometer answered 11/6, 2015 at 13:39 Comment(0)
D
0

Swift 5

If you want to individualize the image flip, you can register each image with the direction you want since the layout direction is a trait:

let leftToRight = UITraitCollection(layoutDirection: .leftToRight)
let rightToLeft = UITraitCollection(layoutDirection: .rightToLeft)
let imageAsset = UIImageAsset()
let leftToRightImage = UIImage(named: "leftToRightImage")!
let rightToLeftImage = UIImage(named: "rightToLeftImage")!
imageAsset.register(leftToRightImage, with: leftToRight)
imageAsset.register(rightToLeftImage, with: rightToLeft)

This is the same as configuring it in the asset catalogue as @SergioM answered.

Desdemona answered 18/11, 2020 at 20:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.