How to send touch events to superview in iOS?

I am doing an iPad app. How do I send touch events to its superview?

I am adding a view which is always some other activity. On that I am adding scrollview to half the screen and in the other half I add another view. It all works fine, now I want to add a button and a small description view that will appear when button is tapped and disappear again when button is tapped again. In order to implement this I took a view which covers the entire view and made its back ground color clear. I place this button and description view into this view but it wont scroll because of transparent view.

  • Xcode can't find header file
  • Show screen on first launch only in iOS
  • What happens with constraints when a view is removed
  • Can UIPinchGestureRecognizer and UIPanGestureRecognizer Be Merged?
  • Swift - How to set initial value for NSUserDefaults
  • Hide navigation bar
  • I want to make this transparent view ignore its events.

    Can this be done?

    2 Solutions Collect From Internet About “How to send touch events to superview in iOS?”

    Set the view’s userInteractionEnabled property to NO. This lets all touches through to the view behind it.

    You can set it in the interface builder/storyboards in the attributes inspector, or in code:

    yourView.userInteractionEnabled = NO;
    

    For when you have a transparent view with a button in it:

    Make a subclass of that view and override the pointInside method. The way I do it, is to give the button’s frame to the subclass as a property, to check for in the pointInside method:

    - (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event {
        BOOL pointInside = NO;
        // Check if the touch is in the button's frame
        if (CGRectContainsPoint(_buttonFrame, point)) pointInside = YES;
    
        return pointInside;
    }
    

    Hope it helps!

    #import <UIKit/UIKit.h>
    
    @interface TransperentView : UIView
    
    @end
    
    #import "TransperentView.h"
    
    @implementation TransperentView
    
    - (id)initWithFrame:(CGRect)frame
    {
      self = [super initWithFrame:frame];
      if (self) 
      {
        // Initialization code
           [self addTransperentView];
           [self addbutton];     
      }
      return self;
    }
    
    -(void)addTransperentView
    {
    
        UIView *aView = [[UIView alloc]initWithFrame:[self.superview bounds]];
        aView.backgroundColor = [UIColor clearColor];   
        [self addSubview:aView];
    }
    
    -(void)addbutton
    {
       UIButton *aButton = [UIButton buttonWithType:UIButtonTypeCustom];
       [aButton setTitle:@"clickMe" forState:UIControlStateNormal];
       [aButton addTarget:self action:@selector(buttonClicked) forControlEvents:UIControlEventTouchUpInside];
       [aButton setFrame:CGRectMake(100, 200, 90, 90)];
       [self addSubview:aButton];     
    }
    
    -(void)buttonClicked
    {
        NSLog(@"button clicked");
    }
    
    -(BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event
    {
      for(UIView *vi in self.subviews)
        {
           if([vi isKindOfClass:[UIButton class]])
            {
                return YES;
            }
        }        
        return NO;
    }
    
    //in main view i have added  a button through xib 
    
    @interface ViewController : UIViewController
    {    
        IBOutlet UIButton *helloButton;   
    }
    
    - (IBAction)whenButtonTapped:(id)sender;
    
    @end
    
    
    #import "ViewController.h"
    #import "TransperentView.h"
    
    @interface ViewController ()
    
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad
    {
    
       [super viewDidLoad];
        // Do any additional setup after loading the view, typically from a nib.     
        TransperentView *tView = [[TransperentView alloc]initWithFrame:self.view.bounds];
    
        [self.view addSubview:tView];  
    }
    
    - (void)didReceiveMemoryWarning
    {  
      [super didReceiveMemoryWarning];
      // Dispose of any resources that can be recreated.   
    }
    
    - (void)dealloc
     {
        [helloButton release];
        [super dealloc];
     }
    
    - (IBAction)whenButtonTapped:(id)sender
    {     
        [helloButton setTitle:@"world" forState:UIControlStateNormal];   
    }
    
    @end