【问题标题】:Hanging (Freezing) issue UISemanticContentAttribute = .ForceRightToLeft挂起(冻结)问题 UISemanticContentAttribute = .ForceRightToLeft
【发布时间】:2017-05-24 17:45:58
【问题描述】:

我在内部切换应用语言 (LTR-RTL),然后重新初始化情节提要。

这是一段代码:

let semanticContentAttribute: UISemanticContentAttribute = language == .Arabic ? .ForceRightToLeft : .ForceLeftToRight

UIView.appearance().semanticContentAttribute = semanticContentAttribute
UINavigationBar.appearance().semanticContentAttribute = semanticContentAttribute

问题是,所有显示的视图控制器在关闭时冻结 3-6 秒。

这是什么原因造成的?

【问题讨论】:

  • 你找到解决办法了吗
  • 问题是应用中的视图太多。 iOS 框架本身有很多内部视图。例如,如果您初始化一个 UISearchBar,那么 UISearchBar 本身就有一个 UITextField、一个 UILabel、一个取消按钮、一个清除按钮、一个背景视图等。因此,将每个视图从 LTR 切换到 RTL 都需要时间。解决方案:我为 UIView 创建了一个扩展,并添加了一个 IBDesignable 属性 - 一个 Bool。因此,当我在 .xib 文件中创建 UIView 时,我只需检查此属性,因此它只会切换此特定视图,而不是其中的每个视图。
  • 感谢您的回答
  • 请多帮忙。您是否使用了任何其他方法而不是 UIView.appearance().semanticContentAttribute。或者你是否覆盖了任何 UIView 方法。

标签: ios iphone swift localization ios9


【解决方案1】:

不支持在 appearance() 代理上设置 semanticContentAttribute。您将遇到许多其他问题和错误,因为该应用仍然认为它运行的语言不是您要覆盖的语言。

向您的应用添加语言切换器只会使其更加混乱;用户希望他们的应用遵循他们设备设置的语言。

【讨论】:

    【解决方案2】:

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

    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 中):

    【讨论】:

      猜你喜欢
      • 2011-02-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-02-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多