iOS How to dismiss UIAlertView with one tap anywhere?

前端 未结 2 636
有刺的猬
有刺的猬 2020-12-06 06:29

I want to dismiss UIAlertView anywhere outside it with one tap. I want to show a UIAlertView without any button.

I have standard UIAlertView codes here, but I need

2条回答
  •  不思量自难忘°
    2020-12-06 06:53

    After showing UIAlertView add to your view controller (or even window) new empty UIView with full screen size. Attach to this wiew UITapGestureRecognizer

    UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTap:)];
    [view addGestureRecognizer:singleTap];
    [singleTap release];
    

    Now in handleSingleTap method you can dismiss UIAlertView and remove this view from window

    -(void)handleSingleTap:(UITapGestureRecognizer *)sender{
        [myAlert dismissWithClickedButtonIndex:0 animated:YES];
        [view removeFromSuperView];
    }
    

提交回复
热议问题