当手机具有RightToLeft本地化时,我禁用了视图旋转(当选择希伯来语并且所有视图从左向右改变位置时的处理):
if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"9.0")){
if (RightToLeft) {
[[UIView appearance] setSemanticContentAttribute:UISemanticContentAttributeForceLeftToRight];
[[UINavigationBar appearance] setSemanticContentAttribute:UISemanticContentAttributeForceLeftToRight];
}
}
一切看起来都不错,但我需要从右向左滑动以返回之前的视图控制器。 如何在ViewControllers之间设置从左向右滑动方向以进行导航?
答案 0 :(得分:0)
我终于为那些继承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将适应于强制语义内容属性。希望对您有所帮助。让我知道您是否有任何问题。
答案 1 :(得分: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中)
当然也使用其中一些疑问。