iOS 8 之后新增了 UIPresentationController 控制器,用于所有的界面轉(zhuǎn)場。在以前,popViewController只適用于iPad栅隐。而現(xiàn)在使用UIPopoverPresentationController不再需要對設(shè)備進(jìn)行判斷了。
這個控制器的用法我還在摸索中玩徊,今天先上一段代碼租悄,做到在iPhone像iPad中的pop彈出視圖效果。
UIPopoverPresentationController不需要你去創(chuàng)建恩袱。蘋果官方文檔: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;//設(shè)置代理
pop.permittedArrowDirections = UIPopoverArrowDirectionAny;//彈出的方向
pop.barButtonItem = self.navigationItem.rightBarButtonItem;//導(dǎo)航欄右側(cè)的按鈕
[self presentViewController:view animated:YES completion:nil];//present即可
}
#pragma mark -- UIPopoverPresentationControllerDelegate
-(UIModalPresentationStyle)adaptivePresentationStyleForPresentationController:(UIPresentationController *)controller {
return UIModalPresentationNone;
}
@end
效果圖如上。
iOS技術(shù)交流群:511860085 歡迎加入畔塔!