ios - 更改 InteractivePopGestureRecognizer 方向

标签 ios objective-c interactivepopgesture

我的应用程序支持英语和阿拉伯语。 interactivePopGestureRecognizer 在使用英语时可以正常工作,即从左向右滑动时,它会弹出 viewController。但是当我使用阿拉伯语时,我已将 semanticContentAttribute 从右更改为左。

if([[[NSUserDefaults standardUserDefaults] objectForKey:@"LanguageCode"] isEqualToString:@"en"])
    {
        [[UIView appearance] setSemanticContentAttribute:UISemanticContentAttributeForceLeftToRight];       //View for English language
    }
    else
    {
        [[UIView appearance] setSemanticContentAttribute:UISemanticContentAttributeForceRightToLeft];       //mirror view for Arabic language
    }

但是interactivePopGestureRecogniser仍然是从左到右。如何更改 interactivePopGestureRecogniser 的方向以使其支持阿拉伯语?我想使用阿拉伯语从右向左滑动到弹出 View Controller 。

最佳答案

如果有人寻求的话,我经过长时间的搜索找到了解决方案。

之前的答案可能会导致 UI 挂起/卡住。

UI 卡住/挂起的原因是,当在 Root View 上执行手势时,UINavigationController 缺少对 Root View Controller 的检查。有几种方法可以解决这个问题,以下是我所做的。

您应该子类化 UINavigationController,这是正确的方法并添加实现,如下所示:

class RTLNavController: UINavigationController, UINavigationControllerDelegate, UIGestureRecognizerDelegate {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        //  Adding swipe to pop viewController
        self.interactivePopGestureRecognizer?.isEnabled = true
        self.interactivePopGestureRecognizer?.delegate = self

        //  UINavigationControllerDelegate
        self.delegate = self
    }
    
    func navigationController(_ navigationController: UINavigationController, willShow viewController: UIViewController, animated: Bool) {
        navigationController.view.semanticContentAttribute = UIView.isRightToLeft() ? .forceRightToLeft : .forceLeftToRight
        navigationController.navigationBar.semanticContentAttribute = UIView.isRightToLeft() ? .forceRightToLeft : .forceLeftToRight
    }

    //  Checking if the viewController is last, if not disable the gesture
    func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {
        if self.viewControllers.count > 1 {
            return true
        }
        
        return false
    }
}

extension UIView {
    static func isRightToLeft() -> Bool {
        return UIView.appearance().semanticContentAttribute == .forceRightToLeft
    }
}

资源:

原始问题:

答案用于解决方案:

其他可能效果更好的解决方案(但它是在 Objective-C 中):

关于ios - 更改 InteractivePopGestureRecognizer 方向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47525211/

相关文章:

objective-c - NSData UIImageJPEGRepresentation 未写入 SQLite fmdb

IOS 7 在不单击消息的情况下对推送通知应用操作

ios - Xcode 11 导出 IPA 时出错找不到 'my.bundle.id' 的配置文件

ios - 将带有可见导航栏的 View Controller 弹出到带有隐藏导航栏的 View Controller 时,带有 interactivePopGestureRecognizer 的黑色区域

swift - 发生交互式弹出手势时搜索栏消失

ios - 带有InteractivePopGestureRecognizer的导航栏标题错误

ios - Firebase 实时数据库,检查 firebase 数据库值是否存在?

ios - UISearchController 在编辑开始时更改宽度

objective-c - Cocoa View Controller 推送/弹出的任何解决方案?

iOS 8 推送通知操作按钮 - handleActionWithIdentifier 中的代码在应用程序处于后台时并不总是运行