1

Systems back button on the navigation bar.
I want this system's button which is on the navigationBar has two different actions.

1. navigationController?.popViewController when device orientation is portrait as default action.
2. rotate device orientation to portrait when device orientation is landscape. UIDevice.current.setValue(UIInterfaceOrientation.portrait.rawValue, forKey: "orientation")


I can do it by create a new UIBarButtonItem and set it to navigationItem.leftBarButtonItem.

But it doesn't have system's left-arrow image and localize string. I need this button has system's left-arrow image and localize string likes system's provide.

I tried to set action like

navigationController?.navigationBar.backItem?.backBarButtonItem?.action = #selector(self.backToPortrait)

It wasn't work.

So how can I set action to this back button?

Amulin
  • 15
  • 6

1 Answers1

1

You cannot add action to that system back button. There are two alternatives.

  1. Create your own back button. Here is an example

  2. Instead of managing orientation change, lock your previous view controller to have portrait mode only. So it can do auto rotation for you, like this

Fangming
  • 24,551
  • 6
  • 100
  • 90