How to dismiss a programmatically generated UIView when the user taps on any part of the iPhone screen

I am working on an application where a popup view is created programmatically. Now the requirement is that the user is deleting somewhere else than this popview, I want to remove this view from the supervisor.

Below is my code to create a view

- (IBAction)Morebtn:(id)sender {
    UIView *cv = [[UIView alloc]initWithFrame:CGRectMake(200, 60, 100, 80)];
   UIButton *label1 = [[UIButton alloc]initWithFrame:CGRectMake(-50,2, 200, 30)];
    [label1 setTitle: @"My Button" forState: UIControlStateNormal];
    label1.titleLabel.font=[UIFont fontWithName:@"SegoeUI" size:12.0];
    [label1 addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];

    [cv addSubview:label1];

    UILabel *label2 = [[UILabel alloc]initWithFrame:CGRectMake(5,20, 200, 30)];
    label2.text = @"Mark as unread";
    label2.font=[UIFont fontWithName:@"SegoeUI" size:12.0];
    [cv addSubview:label2]; //add label2 to your custom view

    [cv setBackgroundColor:[UIColor grayColor]];
    [self.view addSubview:cv];
}

      

Here is my screenshot enter image description here

+3


source to share


5 answers


declare UIView *cv

globally

and Remove

function the same as

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[cv setHidden:YES];
[self.view.superview endEditing:YES];
}

      

choice no 2



@property (strong, nonatomic) UIView * cv;

 -(void)viewDidLoad
{
[super viewDidLoad];

// here add the view

 cv = [[UIView alloc]initWithFrame:CGRectMake(200, 60, 100, 80)];
UIButton *label1 = [[UIButton alloc]initWithFrame:CGRectMake(-50,2, 200, 30)];
[label1 setTitle: @"My Button" forState: UIControlStateNormal];
label1.titleLabel.font=[UIFont fontWithName:@"SegoeUI" size:12.0];
[label1 addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];

[cv addSubview:label1];

UILabel *label2 = [[UILabel alloc]initWithFrame:CGRectMake(5,20, 200, 30)];
label2.text = @"Mark as unread";
label2.font=[UIFont fontWithName:@"SegoeUI" size:12.0];
[cv addSubview:label2]; //add label2 to your custom view

[cv setBackgroundColor:[UIColor grayColor]];

}

      

in button action

- (IBAction)Morebtn:(id)sender {

   [self.view addSubview:cv];

}

      

+2


source


It works 100%



 - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{

        [super touchesBegan:touches withEvent:event];
        [urView removeFromSuperview];
    }

      

+2


source


try it

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{ 
CGPoint locationPoint = [[touches anyObject] locationInView:self.view];

UIView* viewYouWishToObtain = [self hitTest:locationPoint withEvent:event];
 if ([viewYouWishToObtain isKindOfClass: [customView Class]])
    [customView removeFromSuperview];
 }

      

+1


source


Hope this helps you.

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{

[cv removeFromSuperView];

      

//or

[cv setHidden:Yes];
}

      

0


source


its quite simple, just create one method in your UIViewController. will touch Began and write in this method. but before making your cv object global in your view

-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{

    [self.cv removeFromSuperview];
     self.cv=nil;
}

      

0


source







All Articles