iosswiftuisplitviewcontrolleruisplitviewdelegate

Swift iOS- Trigger action when SplitViewController's displayModeButtonItem is tapped


I have a SplitViewController and I'm implementing:

myNavVC?.topViewController?.navigationItem.leftBarButtonItem = splitViewController?.displayModeButtonItem
myNavVC?.topViewController?.navigationItem.leftItemsSupplementBackButton = true

I use it to display the expand button on the view controller when the iPhone 6 Plus or 7 Plus is in .regular orientation. I would like to trigger an action when the button is touched. How I do this?

enter image description here


Solution

  • Simply set action on button.

    myNavVC?.topViewController?.navigationItem.leftBarButtonItem.action = #selector(foo)
    

    Also you need to set the target, the object with receive the action. Probably it would be self in your case.

    myNavVC?.topViewController?.navigationItem.leftBarButtonItem.target = self