覆盖导航堆栈中的后退按钮,同时保持默认后退按钮的外观?
如何仅覆盖一个视图的后退按钮(而不是不同视图中存在的所有后退按钮),以便单击后退按钮时显示根视图控制器?
How do I override the back button for just one view (not for all the back buttons present in different views) such that on click of the back button, the root view controller is shown?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
您需要替换后退按钮并关联一个操作处理程序:
You need to replace the backbutton and associate an action handler:
找到了一个也保留后退按钮样式的解决方案。
将以下方法添加到您的视图控制器中。
现在通过以下方法提供所需的功能:
它所做的只是用透明按钮覆盖后退按钮;)
Found a solution which retains the back button style as well.
Add the following method to your view controller.
Now provide a functionality as needed in the following method:
All it does is to cover the back button with a transparent button ;)
它很旧,但正确的答案是:
不要将 ViewController 推到所有其他视图控制器之上,而是最好仅使用 rootVC 和新 VC 替换整个堆栈。
不是:
但是:
像这样,在后面它只会 popToRoot 因为它是堆栈中的前一个 viewController
It's old, but the correct answer is that:
Instead of pushing your ViewController on top of all the others, you'd better replace the full stack with the rootVC and the new VC only.
Not:
But:
Like that, on back it will just popToRoot because it's the previous viewController in stack
我遇到了类似的问题,我成功地使用了 Sarasranglt 给出的答案。这是 SWIFT 版本。
函数是
I had the similar problem and I successfully used the answer given by Sarasranglt. Here is the SWIFT version.
And the function is
另一种方法是采用 UINavigationControllerDelegate协议。
这些方法会让您知道控制器何时出现,但您必须检查该控制器是您想要的控制器。
Another approach is to adopt UINavigationControllerDelegate Protocol.
Those methods will let you know when a controller appears but you have to check that controller is the controller you want.
为了保持相同的外观,您可以使用:
For keeping same appearance you can use :
遵循 Sarasranglt 在 Objective C 中使用透明按钮的方法以及 Pavle Mijatovic 的早期 swift 版本,这里是 swift 4 版本
:
Following Sarasranglt's method of having a transparent button, in objective C, and Pavle Mijatovic's earlier swift version, here is a swift 4 version:
and
无需制作自定义按钮或无需任何 hack(transparentButton) 只需重写“viewWillDisappear”方法并将代码写入块内。
without making custom button or without any hack(transparentButton) just override 'viewWillDisappear' method and write your code inside the block.
以下代码(针对 Xamarin iOS 用 C# 编写)将用看起来与系统实现类似的自定义按钮替换后退按钮(包括 V 形图标)。
系统“后退”导航将不再触发,您可以随意处理 TouchUpInside。
将其转换为 ObjC 应该不会花很长时间,我会把这个乐趣留给你:)
The following code (written in C# for Xamarin iOS) will replace the back button with a custom one that looks just like the system implementation (chevron icon included).
The system "back" navigation will no longer fire, and you are free to handle the TouchUpInside as you wish.
Converting it to ObjC shouldn't take long, I'll leave that fun to you :)
使用此代码显示自定义后退按钮,在将其标记为重复答案之前请注意
backBarButtonItem
。干杯!
Use this code to show a custom back button, Note the
backBarButtonItem
before marking it as a duplicate answer.Cheers!