1.设置代理
<UIPopoverPresentationControllerDelegate>
2.为它设置popView的灰色透明背景
- (void)play:(UIButton *)sender {
if (self.popVc == nil) {
//创建一个遮罩
UIView *backView = [[UIView alloc]initWithFrame:CGRectMake(0, self.tableViewY, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height)];
self.backView = backView;
backView.backgroundColor = [UIColor blackColor];
backView.alpha = 0;
[self.view addSubview:backView];
[UIView animateWithDuration:0.5 animations:^{
backView.alpha = 0.6;
}];
}
UUPopViewController * popVc = [[UUPopViewController alloc]init];
self.popVc = popVc;
popVc.modalPresentationStyle = UIModalPresentationPopover;
popVc.popDelegate = self;
popVc.preferredContentSize = CGSizeMake(200, 100);
UIPopoverPresentationController *popPreVc = popVc.popoverPresentationController;
self.popPreVc = popPreVc;
popPreVc.sourceView = sender;
popPreVc.permittedArrowDirections = UIPopoverArrowDirectionUp;
popPreVc.sourceRect = CGRectMake(10, 45, 0, 0);
popPreVc.delegate = self;
[self presentViewController:popVc animated:YES completion:nil];
}
3.为了防止backView的y跟着tableView进行滚动
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
self.tableViewY = scrollView.contentOffset.y;
}
4.在popView消失的代理方法里面将背景视图进行消除
-(void)popoverPresentationControllerDidDismissPopover:(UIPopoverPresentationController *)popoverPresentationController{
self.popVc = nil;
[UIView animateWithDuration:0.2 animations:^{
self.backView.alpha = 0;
} completion:^(BOOL finished) {
[self.backView removeFromSuperview];
}];
}
5.此外popView的里面的控制器里面应不能直接控制当前控制器的背景视图的产生,所以应该使用代理,点击时清空背景,清空自己
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
[self dismissViewControllerAnimated:YES completion:nil];
if ([self.popDelegate respondsToSelector:@selector(dismissPopViewAndBackview:)]) {
[self.popDelegate dismissPopViewAndBackview:self];
}
}
6.当前控制器应当成为这个控制器的代理,并实现代理方法
- (void)dismissPopViewAndBackview:(UUPopViewController *)popVc{
[self popoverPresentationControllerDidDismissPopover:self.popPreVc];
}
7.此外还要设置popView的类型(全屏,从64开始等等)
- (UIModalPresentationStyle)adaptivePresentationStyleForPresentationController:(UIPresentationController *)controller{
return UIModalPresentationNone;
}