0

I have a special question and I doesn't find an answer for my case. I have an embedded ViewController in a navigation controller with a Container View. In this Container View is a scrollView. What I want to do is: When I scroll in my ContainerView down I want that the Bar Button Item in the NavigationContoller from my ViewController disappear. When I scroll up it should appear again.

I can hide the whole NavigationBar with the following code, which is in the ContainerViewController.swift-file:

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

}

Is there a similar code that only makes the bar button item disappear? I don't know how to access the bar button item because I can't connect it as an Outlet to the ContainerViewController.swift-file but only to the ViewController.swift-file.

I hope you understand my question and can answer it.

Lukas
  • 251
  • 2
  • 13

2 Answers2

0

Only the ViewController can change what's in the navigation bar, by changing the contents of its own navigationItem. So you would have to send a message from the ContainerViewController to the ViewController (its parent) and the ViewController would do whatever is desired to its navigationItem.

matt
  • 515,959
  • 87
  • 875
  • 1,141
  • Thanks, I thought but that but I don't know how I can detect that something was send to my ViewController. Is there something like an override func, where I can do that? Sorry, I am new in Swift. – Lukas Apr 14 '20 at 20:02
-1

You can access the bar button items of your navigation controller trough the navigationItem property of navigationController. For instance, if you want to hide the left bar button item just set navigationController?.navigationItem.leftBarButtonItem.isHidden = true.

  • Thanks, but that doesn‘t work in my case because I do an Action in a Container View but I want to acces my ViewController where the Navigation Bar Item is. – Lukas Apr 14 '20 at 19:58
  • You can use protocols / singleton to access your ViewController and then hide/show your button. Or even make the same thing, but instead of ViewController, do it for your NavigationController. So you can control the back button where you want. – Daniel Arantes Loverde Apr 14 '20 at 21:56
  • Now I see what you've meant. You can access the correct navigationItem with `parent.navigationItem` where parent is the view controller in which you placed the container view. Then you should be able to access all the bar button items. But since they are proxy objects, you can't set `isHidden`. You could either hold a strong reference to the item and set the navigationItems barButton item to `nil` and reassign it when necessary, or simply set the text of the bar button item to "". – Dreamsparkx3 Apr 16 '20 at 07:04