kevin-lyn / STPopup

STPopup provides STPopupController, which works just like UINavigationController in popup style, for both iPhone and iPad. It's written in Objective-C and compatible with Swift.
MIT License
2.6k stars 345 forks source link

Dismiss by background tap is not working #64

Closed vietnguyen09 closed 8 years ago

vietnguyen09 commented 8 years ago

I've done like readme but it's not working, it's a bug? NSlog trigger show fine but popup never close.

- (void)showPopupDaudit{
    STPopupController *popupController = [[STPopupController alloc] initWithRootViewController:[POPViewController new]];
    [STPopupNavigationBar appearance].tintColor = MP_HEX_RGB(MAINCOLOR);
    popupController.containerView.layer.cornerRadius = 4;
    if (NSClassFromString(@"UIBlurEffect")) {
        UIBlurEffect *blurEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleDark];
        popupController.backgroundView = [[UIVisualEffectView alloc] initWithEffect:blurEffect];
    }
    [popupController.backgroundView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(backgroundViewDidTap:)]];
    [popupController presentInViewController:self];
}
- (IBAction)backgroundViewDidTap:(id)sender
{
    [self.popupController dismiss];
    NSLog(@"Tapping in background.");
}
kevin-lyn commented 8 years ago

@vietnguyen09 think you are referring self.popupController in a wrong view controller. backgroundViewDidTap should be called in your POPViewController.

vietnguyen09 commented 8 years ago

It's error backgroundViewDidTap]: unrecognized selector sent to instance 0x15780b7b0

Main.m

[popupController.backgroundView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(backgroundViewDidTap)]];

Pop.h - (IBAction)backgroundViewDidTap;

Pop.m

- (IBAction)backgroundViewDidTap
{
//    [self.popupController dismiss];
    NSLog(@"Tapping in background2.");
}
kevin-lyn commented 8 years ago

The target is not "self" in this case. Please read more about how to use gesture recognizer.

vietnguyen09 commented 8 years ago

I've just copied your example in readme, I've searched in your example project but its not contains any example about dismiss popup by tapping background.

- (void)showPopupDaudit{
    STPopupController *popupController = [[STPopupController alloc] initWithRootViewController:[POPViewController new]];
    [STPopupNavigationBar appearance].tintColor = MP_HEX_RGB(MAINCOLOR);;

    popupController.containerView.layer.cornerRadius = 4;
    if (NSClassFromString(@"UIBlurEffect")) {
        UIBlurEffect *blurEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleDark];
        popupController.backgroundView = [[UIVisualEffectView alloc] initWithEffect:blurEffect];
    }
    [popupController.backgroundView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:popupController
                                                                                                 action:@selector(backgroundViewDidTap)]];
    [popupController presentInViewController:self];
}

Still error.

vietnguyen09 commented 8 years ago

Please help

MartienB commented 8 years ago

@vietnguyen09 You should set the POPViewController as target, not the STPopupController.

Main.m:

    POPViewController *vc = [[POPViewController alloc] init];
    STPopupController *popupController = [[STPopupController alloc] initWithRootViewController:vc];
    [popupController.backgroundView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:vc action:@selector(backgroundViewDidTap)]];

POPViewController.h:

-(void)backgroundViewDidTap;

POPViewController.m:

- (void)backgroundViewDidTap
{
    [self.popupController dismiss];
}
StasMalinovsky commented 7 years ago

In iOS 11 this solution will not work. Here is the suggestion: [[(UIVisualEffectView *)popupController.backgroundView contentView] addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:vc action:@selector(backgroundViewDidTap)]];