NavigationController在iOS中从右到左 [英] NavigationController Right to Left in iOS

查看:89
本文介绍了NavigationController在iOS中从右到左的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个从右到左的导航控制器,以支持RTL语言. 在阅读了StackOverFlow中的一些帖子后,从右向左推动ViewController使用UINavigationController ,我认为此方法最合适:

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 UINavigationController, I decided that this method is most suitable:

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];
[self.navigationController popViewControllerAnimated:YES];

这将创建一个详细的视图控制器,并将其添加到NavigationController下方的视图控制器堆栈中.弹出NavigationController时,看起来好像是我们实际上在加载DetailedViewController一样,整洁,是吗?

This will create a detailed view controller and add it to the stack of view controllers just below the NavigationController. When popping the NavigationController, it will appear as if we are actually loading the DetailedViewController, neat, eh?

现在我面临两个问题:

1-详细视图控制器不再显示返回按钮.因此,我决定添加一个新按钮来代表它执行此操作.

1- The Detailed View Controller no longer displays the return button. So I decided to add a new button to do this on its behalf.

2-我不知道如何从DetailedViewController返回到NavigationController.

2- I did not know how to return back to the NavigationController from the DetailedViewController.

有什么想法吗?

推荐答案

考虑到您本质上是在入侵导航控制器,为了保持行为的一致性,您需要对其进行进一步的入侵.

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.

我假设可变数组存在于您可以随时访问的位置,为简单起见,将其称为otherNavigationController:

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).

pop函数的代码将如下所示:

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!

这篇关于NavigationController在iOS中从右到左的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆