【问题标题】:Display cancel button when the VC is presented, don't display cancel button when the same VC is pushed出现 VC 时显示取消按钮,按下同一个 VC 时不显示取消按钮
【发布时间】:2021-05-07 16:06:22
【问题描述】:

我有一个需要在两个地方显示的 ViewController。

首先,VC 需要使用其导航栏中的取消按钮来呈现和关闭。

其次,VC 需要推送到导航堆栈(它也以模态方式呈现)并且无法关闭(用户必须使用导航堆栈中的后退按钮)。

我可以在两个地方正确显示 VC,但不能正确使用关闭按钮。当我将取消栏按钮项添加到 VC 时,它在两种情况下都会出现。 如何在 VC 出现时显示取消按钮,在 VC 被按下时隐藏它?

【问题讨论】:

  • 在viewDidLoad中检查self.presentingViewController是否为nil,如果它不是nil,则显示取消按钮,否则不要在导航栏添加任何按钮
  • @SandeepBhandari 这为两者添加了取消按钮。可能是因为 VC 被推送到已经呈现为模态的导航堆栈。
  • 那你为什么不能使用self.navigationController?.presentingViewController != nil?

标签: ios swift uinavigationcontroller navigationbaritems


【解决方案1】:

使用 isBeingPresented 您可以管理取消按钮,

        if self.isBeingPresented {
           debugPrint("Controller is Presented")
        } else {
            debugPrint("Controller is Pushed")
         }

【讨论】:

  • 这不会向显示的 VC 添加取消按钮。是不是因为 VC 提供了一个导航控制器?
  • 试试下面,如果你有 btn 出口 btnCancel.isHidden = !self.isBeingPresented 如果是导航栏 btn 试试下面 self.navigationController.letBarButtonItem = self.isBeingPresented ? btnCancel : 无
  • 您是否绝对需要在两种情况下使用名称“Back”和“Cancel”?或者对您而言重要的是以两种不同方式解除 VC 的正确功能?如果是最后一个,我可以帮你
猜你喜欢
  • 2020-08-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-09-27
  • 2015-04-10
  • 1970-01-01
  • 2013-10-30
相关资源
最近更新 更多