1

I used the following tutorial 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?

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!

enter image description here enter image description here

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 

Also, how do I properly set the action for when the uibarbuttontiem is pressed? I tried the following but it's never called.

func actioncall(sender: AnyObject){
    println("action")
}

Edit 1: I updated the code as follows thanks to agy's response.

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")
}

but the action still fails. The app crashes when I press the uibarbuttontiem and I get the following error message

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[AppName.ProfileViewController actioncall:]: unrecognized selector sent to instance 0x7a07a470'

What's the correct function for the action?

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.

Community
  • 1
  • 1
josealvarado111
  • 565
  • 1
  • 9
  • 24
  • Why,you don't use an IBOutlet to configure the bar button programatically and create it in the storyboard.Use IBAction for that function – Stefan Scoarta Jun 28 '15 at 18:37

1 Answers1

3

Try adding a negative spacer, and change the method targetForAction for addTarget:

    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 = -5; // set the value you need
    self.navigationItem.leftBarButtonItems = [negativeSpacer,leftBarButtonItem]

func actioncall(sender: UIButton!){
    print("Called")
}
agy
  • 2,804
  • 2
  • 15
  • 22
  • Awesome, the negativeSpacer worked perfectly. The action call still fails though. What would be the corresponding func to go with the target "actioncall:"? – josealvarado111 Jun 28 '15 at 18:47
  • It should be func actioncall(sender: UIButton!) – agy Jun 28 '15 at 19:06
  • I tried "func actioncall(sender: UIButton!)", " func actioncall(sender: UIButton?) ", " func actioncall(sender: UIButton) ", "func actioncall(sender: UIBarButtonItem?)", "func actioncall(sender: UIBarButtonItem!)", and"func actioncall(sender: UIBarButtonItem)" and none of them worked. What could I be doing wrong? – josealvarado111 Jun 28 '15 at 22:20
  • I have tried the code and it works perfectly. Where are you defining actioncall? Where are you creating your leftBarButtonItems? – agy Jun 29 '15 at 07:00
  • I'm defining the actioncall in the viewWillAppear function. Is that why it's failing for me? I updated the code above to show that. – josealvarado111 Jun 29 '15 at 15:22
  • I don't see why the action is not called. Is actioncall function within the same class? Why don't you call super on viewwillappear? Why are you setting the leftbarbutton in viewwillappear? – agy Jun 29 '15 at 15:32
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/81886/discussion-between-user2492566-and-agy). – josealvarado111 Jun 29 '15 at 16:19