Search code examples
swiftuikituinavigationbarback-button

Back Button Image - what is it called in UIKit?


I am trying to use UIKit's internal back button image. I have asked this question before and got the technically correct answer that it inherits it from the previous View, BUT if I insert this code you can control the back button on the current View.

// Takeover Back Button
self.navigationItem.hidesBackButton = false
let newBackButton = UIBarButtonItem(title: "<", style: .Plain, target: self, action: "segueBack")
navigationItem.leftBarButtonItem = newBackButton

That gives me a <, "ABC" would give me ABC etc but how do I trigger Swift to put up it's internal Back Button image. The code below doesn't work but I would have thought is along the right lines.

let backImg: UIImage = UIImage(named: "BACK_BUTTON_DEFAULT_ICON")!
navigationItem.leftBarButtonItem!.setBackgroundImage(backImg, forState: .Normal, barMetrics: .Default)

Has anyone worked out how to do this?


Solution

  • Try to add custom view as back button like as

    var backButton = UIButton(frame: CGRectMake(0, 0, 70.0, 70.0))
    var backImage = UIImage(named: "backBtn")
    backButton.setImage(backImage, forState: UIControlState.Normal)
    backButton.titleEdgeInsets = UIEdgeInsetsMake(10.0, 10.0, 10.0, 0.0)
    backButton.setTitle("Back", forState: UIControlState.Normal)
    backButton.addTarget(self, action: "buttonPressed", forControlEvents: UIControlEvents.TouchUpInside)
    var backBarButton = UIBarButtonItem(customView: backButton)
    
    var spacer = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.FixedSpace, target: nil, action: nil)
    spacer.width = -15
    
    self.navigationItem.leftBarButtonItems = [spacer,backBarButton]
    

    enter image description here

    It will look same as iOS back button