1

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.

user1682759
  • 99
  • 10

1 Answers1

3

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.

AaoIi
  • 8,288
  • 6
  • 45
  • 87
  • it's not working! and show error message ('UIApplicationMain' attribute cannot be used in a module that contains top-level code) – Yahya Tabba Feb 14 '19 at 13:34
  • Actually swizzling this getter solved my problem about the back swipe, but it broke other layout in the app. Do you have suggestions what other methods I could swizzle or maybe there are some UserDefaults keys to override? I also set the custom UIView.appearance().semanticContentAttribute and AppleLanguages, AppleTextDirection, NSForceRightToLeftWritingDirection – Taras Nikulin Oct 22 '19 at 17:08
  • @TarasNikulin, you will need to re-layout things if it breaks such as labels in awake from nib or viewdidload. – AaoIi Nov 01 '19 at 20:21
  • App crash when try to open it. iOS 15.* – Muhammed Alkabani May 04 '23 at 06:32
  • @MuhammedAlkabani no crash should happen, we are using the method since 12+ and now 16 without issues, check your code. – AaoIi May 11 '23 at 14:40