Search code examples
iosswipe-gesture

iOS back gesture for RTL Languages


I have enabled back gesture for my iOS application programatically. It is working fine for default region and language. App has feature through which, user can change language i.e custom localization. But for RTL languague like arabic , app content is right to left

   [[UIView appearance] setSemanticContentAttribute:UISemanticContentAttributeForceRightToLeft];

But swiping to go back works from left side. And if I set device language to Arabic then it works from right side.

I want to achieve this programmatically, Please let me know, how can I set swipe direction for back gesture for RTL languages from app, no matter what is device's language.


Solution

  • By using UISemanticContentAttribute it will only effect anything that is UIView.

    To accomplish the swipe direction you'll need to work with UIUserInterfaceLayoutDirection also for the gesture and the complete RTL/LTR support.

    First of all from AppDelegate comment the following line: //@UIApplicationMain

    After that you'll need to create a class called Application subclassing UIApplication and overriding userInterfaceLayoutDirection to be able to switch depending on the language used in the app if its LTR or RTL :

    import UIKit
    
    class Application: UIApplication,UIApplicationDelegate {
    
             override open var userInterfaceLayoutDirection: UIUserInterfaceLayoutDirection {
    
                 get {
    
                     if language == .RTL {
    
                         return .rightToLeft
    
                     }else {
    
                         return .leftToRight
    
                     }
    
                 }
             }
    
         }
    

    And finally create swift file called main.swift and add the following line in it:

    import Foundation
    import UIKit
    
         CommandLine.unsafeArgv.withMemoryRebound(to:
    UnsafeMutablePointer<Int8>.self, capacity: Int(CommandLine.argc)) {argv in
             _ = UIApplicationMain(CommandLine.argc, argv,
    NSStringFromClass(Application.self), NSStringFromClass(AppDelegate.self))
         }
    

    Now userInterfaceLayoutDirection will be called on each screen multiple times, but you have to control if you need RTL or LTR.