Flip UIImageViews for Right to Left Languages

后端 未结 7 1474
悲&欢浪女
悲&欢浪女 2021-02-13 02:31

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 b

相关标签:
7条回答
  • 2021-02-13 02:56

    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.

    0 讨论(0)
  • 2021-02-13 02:56

    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.

    0 讨论(0)
  • 2021-02-13 03:00

    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

    0 讨论(0)
  • 2021-02-13 03:03

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

    0 讨论(0)
  • 2021-02-13 03:06

    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.

    0 讨论(0)
  • 2021-02-13 03:07

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

    0 讨论(0)
提交回复
热议问题