title: Swift properly aligns a UIBarButtonItem that was created programatically
body: <p>I used the following <a href="https://stackoverflow.com/questions/30022780/swiftuibarbuttonitem-in-navigation-bar-programmatically">tutorial</a> to programmatically create a UIBarButtonItem in Swift, but the icon isn't properly aligned to the border. Here's the code for the button. Can anyone tell me how to modify the code below to properly align the UIBarButtonItem Image? </p>
  
  <p>The 1st image shows the location of the programmatically created uibarbuttonitem, and the 2nd image shows the uibarbuttonitem created from storyboard. I want the programmatically created uibarbuttonitem to look like uibarbuttonitem created using storyboard. Thanks for the help!</p>
  
  <p><img src="https://i.stack.imgur.com/EL74f.png" alt="enter image description here">    <img src="https://i.stack.imgur.com/3luFM.png" alt="enter image description here"></p>
  
  <pre><code>var button: UIButton = UIButton()
  button.setImage(UIImage(named: "leftArrow.png"), forState: .Normal)
  button.frame = CGRectMake(0, 0, 40, 40)
  button.targetForAction("actioncall:", withSender: nil)
  
  var leftBarButtonItem:UIBarButtonItem = UIBarButtonItem()
  leftBarButtonItem.customView = button
  self.navigationItem.leftBarButtonItem = leftBarButtonItem 
  </code></pre>
  
  <p>Also, how do I properly set the action for when the uibarbuttontiem is pressed? I tried the following but it's never called. </p>
  
  <pre><code>func actioncall(sender: AnyObject){
      println("action")
  }
  </code></pre>
  
  <p>Edit 1: I updated the code as follows thanks to agy's response.</p>
  
  <pre><code>override func viewWillAppear(animated: Bool) {
      super.viewWillAppear(true)
  
     if (cond){
  
      var button: UIButton = UIButton()
      button.setImage(UIImage(named: "leftArrow.png"), forState: .Normal)
      button.frame = CGRectMake(0, 0, 40, 40)
      button.addTarget(self, action: "actioncall:", forControlEvents: .TouchUpInside)
      var leftBarButtonItem:UIBarButtonItem = UIBarButtonItem()
      leftBarButtonItem.customView = button
  
      var negativeSpacer:UIBarButtonItem = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.FixedSpace, target: nil, action: nil)
      negativeSpacer.width = -20; // set the value you need
      self.navigationItem.leftBarButtonItems  = [negativeSpacer,leftBarButtonItem]
  
     }
  }
  
  func actioncall(sender: UIButton!){
          println("action")
  }
  </code></pre>
  
  <p>but the action still fails. The app crashes when I press the uibarbuttontiem and I get the following error message </p>
  
  <pre><code>*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[AppName.ProfileViewController actioncall:]: unrecognized selector sent to instance 0x7a07a470'
  </code></pre>
  
  <hr>
  
  <p>What's the correct function for the action?</p>
  
  <p>Edit 2: The actioncall is in the same class as where I'm creating the UIBarButtonItem. I'm setting the leftbarbutton on viewwillappear because depending on who views this viewcontroller, the option to go back will replace the previous option located on the leftbarbutton. </p>