2

我的应用程序同时支持英语和阿拉伯语。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它支持阿拉伯语的方向?我想从右向左滑动以使用阿拉伯语弹出视图控制器。

4

2 回答 2

3

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

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

于 2020-09-30T13:47:56.650 回答
2

经过大量试验,唯一对我有用的解决方案是:

斯威夫特 3

extension UIViewController {
    open override func awakeFromNib() {
        super.awakeFromNib()
        navigationController?.view.semanticContentAttribute = .forceRightToLeft
        navigationController?.navigationBar.semanticContentAttribute = .forceRightToLeft
    }
}

您可以排除某些类型的语义属性,例如:

UIView.appearance(whenContainedInInstancesOf: [UITableViewCell.self]).semanticContentAttribute = .forceLeftToRight
于 2018-01-22T14:32:48.557 回答