【iOS UI】popoverPresentationController弹出视图简单使用

popoverPresentationController

preface:

1、支持协议

@interface ViewController ()<UIPopoverPresentationControllerDelegate>

2、添加按钮,并为其绑定方法

    _btn = [UIButton buttonWithType:UIButtonTypeCustom];
    _btn.frame = CGRectMake(150, 100, 100, 100);
    
    [_btn setTitle:@"PopOver" forState:UIControlStateNormal];
    _btn.backgroundColor = [UIColor orangeColor];
    [_btn addTarget:self action:@selector(pop) forControlEvents:UIControlEventTouchDown];
    [self.view addSubview:_btn];

3、pop方法

- (void)pop{
    
    //  初始化弹出控制器
    UIViewController *vc = [UIViewController new];
    
    //  背景色
    vc.view.backgroundColor = [UIColor yellowColor];
    
    //  弹出视图的显示样式
    vc.modalPresentationStyle = UIModalPresentationPopover;
    
    //  1、弹出视图的大小
    vc.preferredContentSize = CGSizeMake(300, 300);
    
    //  弹出视图的代理
    vc.popoverPresentationController.delegate = self;
    
    //  弹出视图的参照视图、从哪弹出
    vc.popoverPresentationController.sourceView = _btn;
    
    //  弹出视图的尖头位置:参照视图底边中间位置
    vc.popoverPresentationController.sourceRect = _btn.bounds;
    
    //  弹出视图的箭头方向
    vc.popoverPresentationController.permittedArrowDirections = UIPopoverArrowDirectionAny;
    
    //  弹出
    [self presentViewController:vc animated:YES completion:nil];
}

4、代理方法

设置点击蒙版是否消失

- (BOOL) popoverPresentationControllerShouldDismissPopover:(UIPopoverPresentationController *)popoverPresentationController{

   return YES;

}

默认返回的是覆盖整个屏幕,需设置成UIModalPresentationNone

- (UIModalPresentationStyle)adaptivePresentationStyleForPresentationController:(UIPresentationController *)controller{

    return UIModalPresentationNone;

}

弹出视图消失后调用的方法

- (void)popoverPresentationControllerDidDismissPopover:(UIPopoverPresentationController *)popoverPresentationController{

    NSLog(@"dismissed");

}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 翻译自“View Controller Programming Guide for iOS”。 1 弹出视图控制器...
    lakerszhy阅读 3,632评论 2 20
  • 翻译自“Collection View Programming Guide for iOS” 0 关于iOS集合视...
    lakerszhy阅读 3,925评论 1 22
  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 12,246评论 4 61
  • 他进藏以后,已经失联第四十三天。 好像秋天没做任何停留,直接坠入了冬。阴冷的雨,没完没了地下得人心烦意乱。 除了等...
    景小菡阅读 2,516评论 44 32
  • 我有一个儿时的伙伴,记得那时我们是最好的朋友。后来因为种种原因就分开了,我的爸妈和她的爸妈也是朋友,所以她有...
    么么lhz阅读 233评论 0 0