【问题标题】:Wrong swipe direction in navigation in Right to Left从右到左导航中的错误滑动方向
【发布时间】:2017-03-21 21:51:59
【问题描述】:

i禁用视图旋转(选择了希伯来语语言的过程,并且所有视图从左到右更改其位置)当手机具有右侧的本地化时:

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

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

【问题讨论】:

  • 您对滑动手势有疑问吗?代码代表什么?它与swipe无关。请提供正确的代码sn-p
  • 我通过这段代码在我的视图控制器中禁用了 RightToLeft 旋转,但是当我们想要返回时它不能解决滑动方向的问题。
  • 嗨,你设置这个是因为你一直想要纵向模式吗?
  • 不,我这样做是因为即使语言设置为希伯来语,我也想要相同的视图位置
  • @Konstantin.Efimenko 我正在寻找相同的。你找到解决办法了吗?

标签: ios objective-c right-to-left


【解决方案1】:

我终于找到了一个为 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 将适应强制语义内容属性。我希望它有所帮助。如果您有任何问题,请告诉我。

【讨论】:

    【解决方案2】:

    如果有人寻求,我在搜索了很长时间后找到了解决方案。

    上一个答案可能会导致 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 中):

    当然还有使用这个问题的一些想法。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-10-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-09-21
      • 2011-03-12
      相关资源
      最近更新 更多