iOS8之UIPopoverPresentationController

UIPopoverPresentationController 是 iOS8 后新增的,相当于pad端的UIPopOverController(ps:本人木有写过,但没吃过猪肉,还没见过猪跑嘛,你懂得。),以后我们也可以在iPhone上使用popOver啦。自己写了一个小demo,大概了熟悉了一下用法。后面有开发需求时会详细补充。

首先要知道一点是,我们并不需要创建一个UIPopoverPresentationController,他相当于UIViewController的属性,因此我们只需要创建UIViewController即可。

举个例子:假设我们要在 ViewController窗体弹窗,弹窗为FirstViewController(kind of UIViewController)我们用两种方式来实现

1、在self.view上创建一个button,给button添加弹窗事件;

2、在导航栏添加barButtonItem,给其添加弹窗时间。

弹窗事件:

1、button

- (void)buttonClick
{
    FirstViewController *firstVC = [[FirstViewController alloc] init];
    firstVC.modalPresentationStyle = UIModalPresentationPopover;
    firstVC.popoverPresentationController.sourceView = popOverButton;  //rect参数是以view的左上角为坐标原点(0,0)
    firstVC.popoverPresentationController.sourceRect = popOverButton.bounds; //指定箭头所指区域的矩形框范围(位置和尺寸),以view的左上角为坐标原点
    firstVC.popoverPresentationController.permittedArrowDirections = UIPopoverArrowDirectionUp; //箭头方向
    firstVC.popoverPresentationController.delegate = self;
    firstVC.preferredContentSize = CGSizeMake(100, 200);
    [self presentViewController:firstVC animated:YES completion:nil];
}

2、barButtonItem

- (void)rightBarButtonItemClick
{
    FirstViewController *firstVC = [[FirstViewController alloc]init];
    firstVC.modalPresentationStyle = UIModalPresentationPopover;
    /**
     *  由于导航上的控件没有指定frame,无法设置sourceView和sourceRect,用下面的。
     */
    firstVC.popoverPresentationController.barButtonItem = self.navigationItem.rightBarButtonItem;
    firstVC.popoverPresentationController.permittedArrowDirections = UIPopoverArrowDirectionUp;
    firstVC.popoverPresentationController.delegate = self;
    firstVC.preferredContentSize = CGSizeMake(100, 200);
    [self presentViewController:firstVC animated:YES completion:nil];
//    firstVC.popoverPresentationController.passthroughViews = nil;
}
注意几点:

1、遵守代理:UIPopoverPresentationControllerDelegate

2、在phone端必须要适配设备,要设置下面代理方法:

/* For iOS8.0, the only supported adaptive presentation styles are UIModalPresentationFullScreen and UIModalPresentationOverFullScreen. */
- (UIModalPresentationStyle)adaptivePresentationStyleForPresentationController:(UIPresentationController *)controller;

iPhone下默认是UIModalPresentationFullScreen,需要手动设置为UIModalPresentationNone,否则,是普通的present全屏显示,没有弹窗效果。iPad不需要。

3、其他设置根据需求翻相关文档即可。

demo:https://github.com/Lynnll/UIPopOverLearning.git



  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值