Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UINavigationBarDelegate shouldPop item weird behavior

Tags:

ios

swift

iphone

I'm trying to prevent some ViewControllers from going back. I have subclassed UINavigationController to make some UI customizations. I conform the UINavigationController subclass to the UINavigationBarDelegate protocol and try to implement the navigationBar:shouldPop method. I have this code:

func navigationBar(_ navigationBar: UINavigationBar, shouldPop item: UINavigationItem) -> Bool {
    print("Popping: \(item.title)")

    return true
}

When I use the swipe from the left way to go back, the method is called and everything works fine. When I press the back button the method is still called but the ViewController doesn't pop. If the ViewController is the second on the stack the back button dissapears like the navigation bar believes that the popping did occur. Can anyone help me understand this behavior?

like image 384
jzlas Avatar asked Jun 10 '26 07:06

jzlas


1 Answers

You have to pop the view manually:

func navigationBar(_ navigationBar: UINavigationBar, shouldPop item: UINavigationItem) -> Bool {
    print("Popping: \(item.title)")

    self.popViewController(animated: true)

    return true
}
like image 72
ucotta Avatar answered Jun 11 '26 21:06

ucotta