Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wrong swipe direction in navigation in Right to Left

I disable views rotation (process when Hebrew language is selected and all views change their positions from left to right) when phone has RightToLeft localization:

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

Everything looks fine, but I need to swipe right-to-left to return in previous view controller. How can I set left to right swipe direction for navigation between ViewControllers?

like image 584
Konstantin.Efimenko Avatar asked Oct 25 '25 14:10

Konstantin.Efimenko


1 Answers

I finally got one working for those who subclassed the UINavigationController class. You should do something like this :

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
    }
}

Dynamically, your NavigationController will adapt to the forced semantic content attribute. I hope it helps. Let me know if you have any problems.

like image 93
Tom Avatar answered Oct 27 '25 12:10

Tom



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!