Why does UINavigationBar steal touch events?

后端 未结 12 2613
耶瑟儿~
耶瑟儿~ 2020-11-27 18:48

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

12条回答
  •  旧时难觅i
    2020-11-27 18:59

    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:

    CustomWindow.h

    @interface CustomWindow: UIWindow 
    @end
    

    CustomWindow.m

    @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++) {
                    //GET THE FINGER LOCATION ON THE SCREEN
                    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;
                    }
                 }
               }  
            }
    
            break;      
    
       default:
            break;
      }
      if(!flag) return; //to do nothing
    
        /*IMPORTANT*/[super sendEvent:(UIEvent *)event];/*IMPORTANT*/
    }
    
    @end
    

    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.

提交回复
热议问题