从右到左导航中的错误滑动方向

3

当手机使用从右到左的本地化语言时,我禁用视图旋转(即所有视图从左侧移动到右侧的过程):

if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"9.0")){
    if (RightToLeft) {
        [[UIView appearance] setSemanticContentAttribute:UISemanticContentAttributeForceLeftToRight];
        [[UINavigationBar appearance] setSemanticContentAttribute:UISemanticContentAttributeForceLeftToRight];
    }
}

一切看起来都很好,但我需要从右向左滑动才能返回上一个视图控制器。 我该如何设置从左到右的滑动方向在视图控制器之间导航?

你的问题是关于滑动手势吗?这段代码代表什么意思?它与滑动无关。请提供正确的代码片段。 - Jeyamahesan
我通过这段代码禁用了我的视图控制器中的从右到左旋转,但它并没有解决我们想要返回时滑动方向的问题。 - Konstantin.Efimenko
嗨,你设置这个是因为你总是想把视图设置为竖屏模式吗? - KrishnaCA
不是,我这么做是因为我希望在语言设置为希伯来语时,视图的位置保持一致。 - Konstantin.Efimenko
尝试这个答案并让我知道:https://stackoverflow.com/a/48383893/1953178 - Amr Hossam
显示剩余2条评论
2个回答

6

我终于为那些从UINavigationController类派生的人做出了一个有效的示例。您应该像这样操作:

final class TestNavigationController: UINavigationController, UINavigationControllerDelegate {

    override func viewDidLoad() {
         super.viewDidLoad()
         self.delegate = self
    }

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

extension UIView {

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

动态地,您的NavigationController将适应强制语义内容属性。希望这能帮到您。如果您有任何问题,请让我知道。


0
我花了很长时间搜索后找到了解决方案,如果有人需要的话。
之前的答案可能会导致UI挂起/冻结。
UI挂起/冻结的原因是当手势在根视图上执行时,UINavigationController缺少对根视图控制器的检查。 有几种方法可以修复它,以下是我所做的。
您应该对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编写的):

当然还可以使用这些问题的一些想法。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接