iphone利用UIPopoverPresentationController实现popover

关于iphone实现popover的效果,网上大多资料都是利用别人写好的库来实现。其实ios8之后有了新方法UIPopoverPresentationController,可以直接实现的。

UIPresentationController管理了所有的UIViewController的presentation。UIPopoverPresentationController是UIPresentationController的一个子类,替代了之前的UIPopoverController,它无需再判断是iphone还是ipad,都会自动进行适配。用于ipad的效果不必多说,但在iphone上,会自动以modal的形式present出来view。这时只要告知代理不要适配,就有了iphone自己的popover。

下面上代码。----<UIPopoverPresentationControllerDelegate>

- (void)TypeNumberBtnClick {

self.typeNumberVC = [[JGTypeNumberViewController alloc]init];

self.typeNumberVC.modalPresentationStyle = UIModalPresentationPopover;//带anchor锚点的样式

self.typeNumberVC.popoverPresentationController.sourceView = self.chooseTypeView.TypeNumberBtn;

//指定相对于Button具体位置

self.typeNumberVC.popoverPresentationController.sourceRect = self.chooseTypeView.TypeNumberBtn.bounds;

//箭头的显示方向(默认指定Any枚举值:自动找最优的方向)

self.typeNumberVC.popoverPresentationController.permittedArrowDirections = UIPopoverArrowDirectionAny;

//设置代理(iPhone需要;iPad不需要)

self.typeNumberVC.popoverPresentationController.delegate = self;

//3.显示弹出控制器

[self presentViewController:self.typeNumberVC animated:YES completion:nil];

}

#pragma mark -- UIPresentationDelegate

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

return UIModalPresentationNone;

}

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

推荐阅读更多精彩内容