iOS RTL 语言的后退手势

iOS back gesture for RTL Languages

我以编程方式为我的 iOS 应用程序启用了后退手势。它适用于默认区域和语言。应用程序具有用户可以更改语言的功能,即自定义本地化。但对于像阿拉伯语这样的 RTL 语言,应用程序内容是从右到左

   [[UIView appearance] setSemanticContentAttribute:UISemanticContentAttributeForceRightToLeft];

但是从左侧滑动返回是可行的。如果我将设备语言设置为阿拉伯语,那么它会在右侧工作。

我想以编程方式实现这一点,请告诉我,无论设备的语言是什么,如何从应用程序为 RTL 语言设置后退手势的滑动方向。

通过使用 UISemanticContentAttribute 它只会影响任何 UIView.

要完成滑动方向,您需要使用 UIUserInterfaceLayoutDirection 也为手势和完整 RTL/LTR支持。

首先从 AppDelegate 注释以下行: //@UIApplicationMain

之后,您需要创建一个 class 名为 Application subclassing UIApplication 并覆盖 userInterfaceLayoutDirection 以能够 如果是 LTR 或 RTL,则根据应用程序中使用的语言进行切换:

import UIKit

class Application: UIApplication,UIApplicationDelegate {

         override open var userInterfaceLayoutDirection: UIUserInterfaceLayoutDirection {

             get {

                 if language == .RTL {

                     return .rightToLeft

                 }else {

                     return .leftToRight

                 }

             }
         }

     }

最后创建名为 main.swift 的 swift 文件并添加以下内容 其中一行:

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))
     }

现在 userInterfaceLayoutDirection 将在每个屏幕上调用 多次,但您必须控制是否需要 RTL 或 LTR。