前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >使iPhone也可以拥有iPad的pop效果

使iPhone也可以拥有iPad的pop效果

作者头像
Raindew
发布2018-06-14 14:34:48
7830
发布2018-06-14 14:34:48
举报
iOS 8 之后新增了 UIPresentationController 控制器,用于所有的界面转场。在以前,popViewController只适用于iPad。而现在使用UIPopoverPresentationController不再需要对设备进行判断了。

这个控制器的用法我还在摸索中,今天先上一段代码,做到在iPhone像iPad中的pop弹出视图效果。 UIPopoverPresentationController不需要你去创建。苹果官方文档:In nearly all cases, you use this class as-is and do not create instances of it directly. 下面是代码:

代码语言:javascript
复制
#import "ViewController.h"
#import "TableViewController.h"
@interface ViewController ()<UIPopoverPresentationControllerDelegate>
@end
@implementation ViewController
- (void)viewDidLoad {  
  [super viewDidLoad]; 
   self.title = @"Presentation"; 
   UIBarButtonItem *rightBar = [[UIBarButtonItem alloc] initWithTitle:@"pop" style:UIBarButtonItemStyleDone target:self action:@selector(popView:)];  
   self.navigationItem.rightBarButtonItem = rightBar;
}
- (void)popView:(UIBarButtonItem *)rightBar {  
  TableViewController *view = [[TableViewController alloc] init];   
  view.preferredContentSize = CGSizeMake(120, 200);//popover视图的大小
  view.modalPresentationStyle = UIModalPresentationPopover;//如果没有这句,pop不会被初始化  
  UIPopoverPresentationController *pop = view.popoverPresentationController;    pop.delegate = self;//设置代理  
  pop.permittedArrowDirections = UIPopoverArrowDirectionAny;//弹出的方向    
  pop.barButtonItem = self.navigationItem.rightBarButtonItem;//导航栏右侧的按钮  
  [self presentViewController:view animated:YES completion:nil];//present即可
}
#pragma mark -- UIPopoverPresentationControllerDelegate
-(UIModalPresentationStyle)adaptivePresentationStyleForPresentationController:(UIPresentationController *)controller {  
 return UIModalPresentationNone;
}
@end

效果图如上。

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2017.02.16 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • iOS 8 之后新增了 UIPresentationController 控制器,用于所有的界面转场。在以前,popViewController只适用于iPad。而现在使用UIPopoverPresentationController不再需要对设备进行判断了。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档