快速命令以在ContainerController中通过动作隐藏ViewController中的Bar Button项?

问题描述 投票:0回答:2

我有一个特殊的问题,但找不到我的案子的答案。我在带有容器视图的导航控制器中具有嵌入式ViewController。在此容器视图中是一个scrollView。我想要做的是:当我向下滚动ContainerView时,我希望ViewController中NavigationContoller中的Bar Button项消失。当我向上滚动时,它应该会再次出现。

我可以使用以下代码隐藏整个NavigationBar,这些代码位于ContainerViewController.swift文件中:

func scrollViewWillEndDragging(_ scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>) {
    print("123")
    if(velocity.y>0) {
        UIView.animate(withDuration: 0.5, delay: 0, options: UIView.AnimationOptions(), animations: {
            self.navigationController?.setNavigationBarHidden(true, animated: true)
            print("Hide")
        }, completion: nil)

    } else {
        UIView.animate(withDuration: 0.5, delay: 0, options: UIView.AnimationOptions(), animations: {
            self.navigationController?.setNavigationBarHidden(false, animated: true)
            print("Unhide")
        }, completion: nil)
      }

}

是否有类似的代码仅使条形按钮项消失?我不知道如何访问栏按钮项,因为我无法将其作为插座连接到ContainerViewController.swift文件,而只能连接到ViewController.swift文件。

希望您能理解我的问题并可以回答。

swift uiscrollview uibarbuttonitem uislider
2个回答
0
投票

只有ViewController可以通过更改其自己的navigationItem的内容来更改导航栏中的内容。因此,您必须将一条消息从ContainerViewController发送到ViewController(其parent),然后ViewController将对其navigationItem进行所需的操作。


-1
投票

您可以通过navigationItemnavigationController属性访问导航控制器的条形按钮项目。例如,如果要隐藏左栏按钮项目,只需设置navigationController?.navigationItem.leftBarButtonItem.isHidden = true

© www.soinside.com 2019 - 2024. All rights reserved.