NavigationController Right to Left in iOS

前端 未结 2 1397
离开以前
离开以前 2021-02-10 15:39

I am buiding a Right to Left navigation controller to support RTL Languages. After reading some posts in StackOverFlow Push ViewController from Right To Left with UINavigationCo

2条回答
  •  伪装坚强ぢ
    2021-02-10 16:10

    Considering that you're essentially hacking into the navigation controller, in order to keep the behavior consistent, you need to hack it some more.

    Popping a navigation controller releases it from the memory, so you would probably need another array or stack containing popped controllers (pushed controllers from the users perspective, because as far as I see it, you're popping whenever you need to push, and pushing whenever you need to pop). In that array/stack, you would keep the controllers you need to come back to, pushing them into the array/stack just before you pop them.

    I'll assume that the mutable array exists somewhere you can access it at all times, and call it otherNavigationController for the sake of simplicity:

    DetailedViewController *DVC = [[DetailedViewController alloc]initWithNibName:@"DetailedViewController" bundle:nil];
    NSMutableArray *vcs = [NSMutableArray arrayWithArray:self.navigationController.viewControllers];
    [vcs insertObject:DVC atIndex:[vcs count]-1];
    [self.navigationController setViewControllers:vcs animated:NO];
    [otherNavigationController addObject:self];
    [self.navigationController popViewControllerAnimated:YES];
    

    As for the second part of your question, you would need to add custom back button because the default one wouldn't work for you. The custom button should on press push a view controller from the top of the aforementioned array/stack (pop it from the users perspective).

    The code for the pop function would go something like this:

    UIViewController *previousViewController = [otherNavigationController lastObject];
    [otherNavigationController removeLastObject];
    [self.navigationController pushViewController:previousViewController animated:YES];
    

    Disclaimer: The code here is untried and untested and I'm not even sure this would work, but it should get you on your way. Good luck!

提交回复
热议问题