Search code examples
iosiphoneuikituibarbuttonitemuitoolbar

For iPhone – but not iOS Simulator – UIToolbar's UIBarButtonItems will highlight only on long press, rather than on tap


I have an iPhone 5s running iOS 10.3.1. In a new UIViewController, I set up self.toolbarItems as follows:

self.toolbarItems = [
  UIBarButtonItem(title: "Hi", style: .plain, target: self, action: #selector(sayHi)),
  UIBarButtonItem(barButtonSystemItem: .refresh, target: self, action: #selector(redoIt))
]

On iOS Simulator, a single click to either of these will instantly cause the barButtonItem to highlight. However, on my iPhone, the buttons only highlight upon performing a long press (maybe half a second long); single taps activate the button's action, but do not highlight it. Is this expected behaviour?


Solution

  • Yes, It's expected UIBarButtonItem behaviour. You can try different bar buttons in default apple apps like, contacts, photos etc. You will see the same behaviour, you have to press and hold it for a bit to see the highlighted state.

    If You really want it, you have to do a custom implementation. Have a look at the UIControlEvents