By Alexander

2012-01-31 12:58:31 8 Comments

I have a custom UIButton with UILabel added as subview. Button perform given selector only when I touch it about 15points lower of top bound. And when I tap above that area nothing happens.

I found out that it hasn't caused by wrong creation of button and label, because after I shift the button lower at about 15 px it works correctly.

UPDATE I forgot to say that button located under the UINavigationBar and 1/3 of upper part of the button don't get touch events.

Image was here

View with 4 buttons is located under the NavigationBar. And when touch the "Basketball" in top, BackButton get touch event, and when touch "Piano" in top, then rightBarButton (if exists) get touch. If not exists, nothing happened.

I didn't find this documented feature in App docs.

Also I found this topic related to my problem, but there is no answer too.


@Ilya Shevyryaev 2019-05-24 14:49:44

The following worked for me:

self.navigationController?.isNavigationBarHidden = true

@Alexander 2012-03-15 12:08:31

I found out the answer here(Apple Developer Forum).

Keith at Apple Developer Technical Support, on 18th May 2010 (iPhone OS 3):

I recommend that you avoid having touch-sensitive UI in such close proximity to the nav bar or toolbar. These areas are typically known as "slop factors" making it easier for users to perform touch events on buttons without the difficulty of performing precision touches. This is also the case for UIButtons for example.

But if you want to capture the touch event before the navigation bar or toolbar receives it, you can subclass UIWindow and override: -(void)sendEvent:(UIEvent *)event;

Also I found out,that when I touch the area under the UINavigationBar, the location.y defined as 64,though it was not. So I made this:


@interface CustomWindow: UIWindow 


@implementation CustomWindow
- (void) sendEvent:(UIEvent *)event
  BOOL flag = YES;
  switch ([event type])
   case UIEventTypeTouches:
        //[self catchUIEventTypeTouches: event]; perform if you need to do something with event         
        for (UITouch *touch in [event allTouches]) {
          if ([touch phase] == UITouchPhaseBegan) {
            for (int i=0; i<[self.subviews count]; i++) {
                CGPoint location = [touch locationInView:[self.subviews objectAtIndex:i]];

                //REPORT THE TOUCH
                NSLog(@"[%@] touchesBegan (%i,%i)",  [[self.subviews objectAtIndex:i] class],(NSInteger) location.x, (NSInteger) location.y);
                if (((NSInteger)location.y) == 64) {
                    flag = NO;


  if(!flag) return; //to do nothing

    /*IMPORTANT*/[super sendEvent:(UIEvent *)event];/*IMPORTANT*/


In AppDelegate class I use CustomWindow instead of UIWindow.

Now when I touch area under navigation bar, nothing happens.

My buttons still don't get touch events,because I don't know how to send this event (and change coordinates) to my view with buttons.

@Natan R. 2013-06-05 14:53:44

Do you know what's most strange here? That in the iPad, instead of 64, the origin that should be flagged to NO is 44...

@Alexander 2013-06-09 07:48:41

Perhaps this is so. I have not checked on the iPad. And yes, this is queerly.

@Tom Hamming 2015-01-16 22:41:58

This even happens outside the containing view controller's bounds (my question). I've filed this as radar 19504573.

@David 2017-06-14 20:26:47

The Apple link doesn't work. Is there another link to active Apple documentation which gives this advice? I haven't found it in the HIG.

@Cœur 2017-06-19 04:10:12

@David it works for people with a developer account. Credit to the quote is given to Keith from Apple Developer Technical Support.

@David 2017-06-20 15:56:47

I have a developer account and the link does not work. Takes you to login screen and then, after login, to the top developer account page.

@Kevin Bel 2017-04-27 19:07:45

The solution for me was the following one:

First: Add in your application (It doesn't matter where you enter this code) an extension for UINavigationBar like so: The following code just dispatch a notification with the point and event when the navigationBar is being tapped.

extension UINavigationBar {
open override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? { NSNotification.Name(rawValue: "tapNavigationBar"), object: nil, userInfo: ["point": point, "event": event as Any])
    return super.hitTest(point, with: event)

Then in your specific view controller you must listen to this notification by adding this line in your viewDidLoad:

NotificationCenter.default.addObserver(self, selector: #selector(tapNavigationBar), name: NSNotification.Name(rawValue: "tapNavigationBar"), object: nil)

Then you need to create the method tapNavigationBar in your view controller as so:

func tapNavigationBar(notification: Notification) {
    let pointOpt = notification.userInfo?["point"] as? CGPoint
    let eventOpt = notification.userInfo?["event"] as? UIEvent?
    guard let point = pointOpt, let event = eventOpt else { return }

    let convertedPoint = YOUR_VIEW_BEHIND_THE_NAVBAR.convert(point, from: self.navigationController?.navigationBar)
    if YOUR_VIEW_BEHIND_THE_NAVBAR.point(inside: convertedPoint, with: event) {
        //Dispatch whatever you wanted at the first place.

PD: Don't forget to remove the observation in the deinit like so:

deinit {

That's it... That's a little bit 'tricky', but it's a good workaround for not subclassing and getting a notification anytime the navigationBar is being tapped.

@Victor Choy 2016-08-31 12:15:35

Give a extension version according to Bart Whiteley. No need to subclass.

@implementation UINavigationBar(Xxxxxx)

- (UIView*)hitTest:(CGPoint)point withEvent:(UIEvent *)event
    UIView *v = [super hitTest:point withEvent:event];
    return v == self ? nil: v;


@Greg 2018-01-23 23:44:43

It is not safe to add category methods with the same name as existing methods on an object. (see

@Chris Villa 2016-06-02 15:04:00

An alternative solution that worked for me, based on the answer provided by Alexandar:

self.navigationController?.barHideOnTapGestureRecognizer.enabled = false

Instead of overriding the UIWindow, you can just disable the gesture recogniser responsible for the "slop area" on the UINavigationBar.

@Jonatha ANTOINE 2017-04-07 22:08:08

looked promising but not working (ios10, tested on a device)

@skkrish 2017-04-20 16:19:54

Saviour, simplest solution, struggled entire day for this, Thanks Chris.

@Greg 2018-01-23 22:05:05

Also does not work in iOS 9 simulator.

@kandelvijaya 2015-09-25 08:55:53

I just wanted to share another prospective to solving this problem. This is not a problem by design, but it was meant to help user get back or navigate. But we need to put things tightly in or below nav bar and things look sad.

First lets look at the code.

class MyNavigationBar: UINavigationBar {
private var secondTap = false
private var firstTapPoint = CGPointZero

override func pointInside(point: CGPoint, withEvent event: UIEvent?) -> Bool {
    if !self.secondTap{
        self.firstTapPoint = point

        self.secondTap = !self.secondTap

    return  super.pointInside(firstTapPoint, withEvent: event)


You might be see why am i doing second touch handling. There is the recipe to the solution.

Hit test is called twice for a call. The first time the actual point on the window is reported. Everything goes well. On the second pass, this happens.

If system sees a nav bar and the hit point is around 9 pixels more on Y side, it tries to decrease that gradually to below 44 points which is where the nav bar is.

Take a look at the screen to be clear.

enter image description here

So theres a mechanism that will use nearby logic to the second pass of hittest. If we can know its second pass and then call the super with first hit test point. Job done.

The above code does that exactly.

@Cœur 2017-06-19 04:37:26

if hit test is called an odd number of times for some reason, then it would break, isn't it?

@Bart Whiteley 2014-10-09 20:28:45

Subclass UINavigationBar and add this method. It will cause taps to be passed through unless they are tapping a subview (such as a button).

 -(UIView*) hitTest:(CGPoint)point withEvent:(UIEvent *)event
    UIView *v = [super hitTest:point withEvent:event];
    return v == self? nil: v;

@Victor Choy 2016-08-31 12:13:26

A clean and smart way. It works for me

@mxcl 2016-12-08 21:54:42

Notably this will break any non-customized UIBarButtonitems in your navbar, eg. the back button.

@devliubo 2018-11-06 12:10:50

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event { UIView *hitView = [super hitTest:point withEvent:event]; if ([hitView isKindOfClass:[UIControl class]]) { return hitView; } else { return nil; } } @mxcl

@wfxiang 2013-05-14 16:42:15

Extending Alexander's solution:

Step 1. Subclass UIWindow

@interface ChunyuWindow : UIWindow {
    NSMutableArray * _views;

    UIView *_touchView;

- (void)addViewForTouchPriority:(UIView*)view;
- (void)removeViewForTouchPriority:(UIView*)view;

// .m File
// #import "ChunyuWindow.h"

@implementation ChunyuWindow
- (void) dealloc {
    [super dealloc];

- (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event {
    if (UIEventSubtypeMotionShake == motion
        && [TTNavigator navigator].supportsShakeToReload) {
        // If you're going to use a custom navigator implementation, you need to ensure that you
        // implement the reload method. If you're inheriting from TTNavigator, then you're fine.
        TTDASSERT([[TTNavigator navigator] respondsToSelector:@selector(reload)]);
        [(TTNavigator*)[TTNavigator navigator] reload];

- (void)addViewForTouchPriority:(UIView*)view {
    if ( !_views ) {
        _views = [[NSMutableArray alloc] init];
    if (![_views containsObject: view]) {
        [_views addObject:view];

- (void)removeViewForTouchPriority:(UIView*)view {
    if ( !_views ) {

    if ([_views containsObject: view]) {
        [_views removeObject:view];

- (void)sendEvent:(UIEvent *)event {
    if ( !_views || _views.count == 0 ) {
        [super sendEvent:event];

    UITouch *touch = [[event allTouches] anyObject];
    switch (touch.phase) {
        case UITouchPhaseBegan: {
            for ( UIView *view in _views ) {
                if ( CGRectContainsPoint(view.frame, [touch locationInView:[view superview]]) ) {
                    _touchView = view;
                    [_touchView touchesBegan:[event allTouches] withEvent:event];
        case UITouchPhaseMoved: {
            if ( _touchView ) {
                [_touchView touchesMoved:[event allTouches] withEvent:event];
        case UITouchPhaseCancelled: {
            if ( _touchView ) {
                [_touchView touchesCancelled:[event allTouches] withEvent:event];
                _touchView = nil;
        case UITouchPhaseEnded: {
            if ( _touchView ) {
                [_touchView touchesEnded:[event allTouches] withEvent:event];
                _touchView = nil;

        default: {

    [super sendEvent:event];


Step 2: Assign ChunyuWindow instance to AppDelegate Instance

Step 3: Implement touchesEnded:widthEvent: for view with buttons, for example:

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    [super touchesEnded: touches withEvent: event];

    UITouch *touch = [touches anyObject];
    CGPoint point = [touch locationInView: _buttonsView]; // a subview contains buttons
    for (UIButton* button in _buttons) {
        if (CGRectContainsPoint(button.frame, point)) {
            [self onTabButtonClicked: button];

Step 4: call ChunyuWindow's addViewForTouchPriority when the view we care about appears, and call removeViewForTouchPriority when the view disappears or dealloc, in viewDidAppear/viewDidDisappear/dealloc of ViewControllers, so _touchView in ChunyuWindow is NULL, and it is the same as UIWindow, having no side effects.

@HDdeveloper 2014-01-24 09:53:25

This solved my problem..

I added hitTest:withEvent: code to my navbar subclass..

 -(UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
        int errorMargin = 5;// space left to decrease the click event area
        CGRect smallerFrame = CGRectMake(0 , 0 - errorMargin, self.frame.size.width, self.frame.size.height);
        BOOL isTouchAllowed =  (CGRectContainsPoint(smallerFrame, point) == 1);

        if (isTouchAllowed) {
            self.userInteractionEnabled = YES;
        } else {
            self.userInteractionEnabled = NO;
        return [super hitTest:point withEvent:event];

@Andrei Radulescu 2012-05-15 09:19:11

I noticed that if you set userInteractionEnabled to OFF, the NavigationBar doesn't "steal" the touches anymore.

So you have to subclass your UINavigationBar and in your CustomNavigationBar do this:

-(UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {

    if ([self pointInside:point withEvent:event]) {
        self.userInteractionEnabled = YES;
    } else {
        self.userInteractionEnabled = NO;

    return [super hitTest:point withEvent:event];

Info about how to subclass UINavigationBar you can find here.

@Jonas Alves 2013-03-24 01:34:04

This only works on the Simulator

@jtbandes 2013-03-27 08:52:41

Disabling user interaction seems to work on the device for me. I'm testing with an iPhone 5 and iOS 6.1.2. Subclassing isn't necessary though; you can just disable user interaction externally.

@Yunus Nedim Mehel 2013-06-03 14:24:37

very nice solution, since I cannot disable user interaction (there was a button), thanks.

@tom 2014-02-12 15:13:41

This solution caused a bug for me where sometimes when navigating back only the navigation bar animated back, but the view wasn't removed or became black.

@Radu Simionescu 2015-09-17 10:26:05

still works in ios 8.4 today!

@Ortwin Gentz 2016-03-04 20:15:23

doesn't work in iOS 9

@LaborEtArs 2016-05-16 16:28:28

Works perfectly with iOS 9.3 on simulator as well as on device. I've created an UINavigationBar subclass and replaced the default class directly in IB.

@Mundi 2012-01-31 14:03:07

Your labels are huge. They start at {0,0} (the left top corner of the button), extend over the entire width of the button and have a height of the entire view. Check your frame data and try again.

Also, you have the option of using the UIButton property titleLabel. Maybe you are setting the title later and it goes into this label rather than your own UILabel. That would explain why the text (belonging to the button) would work, while the label would be covering the rest of the button (not letting the taps go through).

titleLabel is a read-only property, but you can customize it just as your own label (except perhaps the frame) including text color, font, shadow, etc.

@Alexander 2012-02-01 04:55:57

Thanks, but I found out that wasn't the problem of creation button and label. And now I use titleLabel instead of my label.

@aksani56 2012-01-31 13:23:16

There are 2 things that might be causing problems.

  1. Did you try setUserInteractionEnabled:NO for the label.

  2. Second thing i think might work is apart from that after adding label on top of button you can send the label to back (it might work, not sure although)

    [button sendSubviewToBack:label];

Please let me know if the code works :)

@Alexander 2012-02-01 03:33:31

Thanks, but code works same way, area above label text don't get touches... but under label text it works normal.

Related Questions

Sponsored Content

2 Answered Questions

[SOLVED] Highlighting a UIView similar to UIButton

4 Answered Questions

UIButton is not registering touches

1 Answered Questions

1 Answered Questions

3 Answered Questions

[SOLVED] CAEmitterLayer emits random unwanted particles on touch events

4 Answered Questions

[SOLVED] UIButton not getting touch events inside a custom UITableViewCell

  • 2010-12-24 19:31:26
  • user210504
  • 2103 View
  • 3 Score
  • 4 Answer
  • Tags:   iphone ipad ios4

1 Answered Questions

3 Answered Questions

How to catch any touch event on a UIButton that is a subview of a UITableView

1 Answered Questions

[SOLVED] iPhone, Capture all touch events in a dimming view

  • 2011-12-17 06:54:58
  • KrzyCube
  • 697 View
  • 0 Score
  • 1 Answer
  • Tags:   iphone ios

3 Answered Questions

Sponsored Content