0
votes

I added UIButton with custom image and no text to Navigation Bar in one of my View Controllers. In that Navigation bar is also default button Back.

I connected that UIButton with Connection: Action, Type: UIButton, Event: Touch Up Inside and Arguments: Sender.

When I click on that button nothing happens, it's not registering touch at all. Button Back is working normally.

enter image description here

Why is this happening? How to fix it?

4
you did not take a photo, I suspect the button's size you did not set , and it may be too small - aircraft
It's not to small. Size is 35x35. You can see it clearly. - user3847113
can you take the photo in your post? - aircraft
Are you adding it via navigationController?.navigationBar.addSubview(myButton) or navigationItem.rightBarButtonItem = UIBarButtonItem(customView: myButton)? - paulvs
@paulvs I just dragged it in Main.storyboard to Navigation Bar and then connected it with View Controller just like you normally connect button. - user3847113

4 Answers

0
votes

Try this:

  1. Since you are adding it to navigation bar it must be a UIBarButtonItem. So the action outlet you are connecting to it must have a type UIBarButtonItem and not UIButton.

  2. Check if the outlet of the UIBarButtonItem is connected properly.

  3. Check if the user interaction of UIBarButtonItem is enabled.

0
votes

When I click on that button nothing happens, it's not registering touch at all.

99% of the time when I see this complaint, the reason is that you've somehow placed the button outside its superview. A button outside its superview is visible (by default) but not touchable. (Sometimes this happens because the superview itself has never been given a size and so has zero size; other times it is for some other reason.)

I can't tell you any more because you didn't tell any more (i.e. you didn't give details of how this button is getting into the navigation bar). But I'm willing to bet that if you dig deeper you'll find that my guess is right.

0
votes

I solved it by first adding Navigation Item to Navigation Bar and then adding Button to Navigation Item.

0
votes

To add button on navigation bar you have to add like this:

 let leftButton = UIBarButtonItem(title: "abc", style: .Plain, target: self, action: #selector(ViewController.buttonClicked(_:)))
 let navigationItem = UINavigationItem()
 navigationItem.titleView = UIImageView(image: UIImage(named: "image.png"))
 navigationItem.leftBarButtonItem = leftButton

This is for left button. You can add right button in the same way.