Flip UIImageViews for Right to Left Languages

醉酒当歌 提交于 2019-12-04 19:12:47

问题


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?


回答1:


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




回答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;



回答3:


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




回答4:


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.




回答5:


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.



来源:https://stackoverflow.com/questions/20354498/flip-uiimageviews-for-right-to-left-languages

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!