美文网首页iOS DeveloperiOS Development
使iPhone也可以拥有iPad的pop效果

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

作者: Raindew | 来源:发表于2017-02-16 15:54 被阅读313次

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 欢迎加入!

相关文章

网友评论

    本文标题:使iPhone也可以拥有iPad的pop效果

    本文链接:https://www.haomeiwen.com/subject/jmlhwttx.html