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

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.
下面是代码:

#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

效果图如上。
iOS技术交流群:511860085 欢迎加入!

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容