0

Displaying an UIViewController modally from one of the View Controllers(which are in a Navigation controller) code :

self.present(viewController, animated: true, completion: nil)

Within the modal ViewController, a custom Navigation Bar along with navigation item is added. In which, i'm trying to display the Back Button.

let backButton = UIBarButtonItem()
backButton.title = "Back"
self.navigItem.backBarButtonItem = backButton  //self.navigItem is the custom Navigation Item

Is there something im missing here?

thanks in advance, Rajesh

Rajesh Rs
  • 1,301
  • 5
  • 24
  • 48
  • 1
    present VC does not embed with Navigationcontroller, if you need embed your VC in nav controller and present your VC, else progrmatically create navigation bar in presented VC – Anbu.Karthik Aug 31 '17 at 11:24
  • VC needs to be presented modally not added in the nav controller, Have added the custom Navigation Bar in the StoryBoard for this viewcontroller. Im able to add/show other bar button items except back button. – Rajesh Rs Aug 31 '17 at 11:27
  • see this example https://stackoverflow.com/questions/21448766/adding-navigation-bar-programmatically-ios/21448861#21448861 – Anbu.Karthik Aug 31 '17 at 11:33
  • Im able to add the left button item as Back, can i access the system back icon to set or i need to add as part of the image assets and access? – Rajesh Rs Aug 31 '17 at 11:38
  • add as part of the image assets and access? -- is better – Anbu.Karthik Aug 31 '17 at 11:40
  • whats the concept you tried – Anbu.Karthik Aug 31 '17 at 11:45
  • Added a Left "Back" Bar Button Item to the Navigation Bar, Now trying to add Back icon to image assets so that i can set it to the button. – Rajesh Rs Aug 31 '17 at 11:48
  • if you want to handle the back button action use custom else no need – Anbu.Karthik Aug 31 '17 at 11:49

2 Answers2

0
 let btnleft : UIButton = UIButton(frame: CGRect(x:0, y:0, width:35, 
                           height:35))
 btnleft.contentMode = .center
 btnleft.setImage(Set_Local_Image("arrow_left"), for: .normal)
 btnleft.addTarget(self, action: #selector(YOUR_ACTION), for: .touchDown)

 let backBarButon: UIBarButtonItem = UIBarButtonItem(customView: btnleft)
 self.navigationItem.setLeftBarButtonItems([menuBarButon], animated:false)
cole
  • 3,147
  • 3
  • 15
  • 28
0

In Swift3 try this code on the viewDidLoad

    self.navigationController?.navigationBar.backIndicatorImage = UIImage(named: "arrow_left")
    self.navigationController?.navigationBar.backIndicatorTransitionMaskImage = UIImage(named: "arrow_left")
    UIBarButtonItem.appearance().setBackButtonTitlePositionAdjustment(UIOffsetMake(0, -60), for:UIBarMetrics.default)
    self.navigationController?.navigationBar.tintColor = UIColor.white
    self.navigationItem.hidesBackButton = false
Wide Angle Technology
  • 1,184
  • 1
  • 8
  • 28