1.回顧UIPopoverController的使用,下面這份代碼只能在ipad下運行
// 初始化控制器,SecondViewController類繼承自UIViewController
SecondViewController *vc = [[SecondViewController alloc] init];
// 把vc包裝成UIPopoverController
UIPopoverController *popover = [[UIPopoverController alloc] initWithContentViewController:vc];
// 設置popover的指向,
// 指向當前控制上button按鈕,所以FromRect后跟bounds
// 如果是指向當前控制的View,則FromRect后跟frame
[popover presentPopoverFromRect:self.button.bounds inView:self.button permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
2. 運用UIPopoverPresentationController,下面這份代碼在iphone和ipad中,都可以正常運行
2.1 在iphone中是常見的modal方式,也就是從屏幕底部爬上來,而在ipad中,是popover的那種方式
// 初始化控制器
SecondViewController *vc = [[SecondViewController alloc] init];
// modal出來是個popover
vc.modalPresentationStyle = UIModalPresentationPopover;
// 取出vc所在的UIPopoverPresentationController
vc.popoverPresentationController.sourceView = self.button;
vc.popoverPresentationController.sourceRect = self.button.bounds;
[self presentViewController:vc animated:YES completion:nil];
2.2 在iphone中,上面這份代碼等同於下面:
SecondViewController *vc = [[SecondViewController alloc] init];
/* 相當於中間3行代碼不存在
vc.modalPresentationStyle = UIModalPresentationPopover;
vc.popoverPresentationController.sourceView = self.button;
vc.popoverPresentationController.sourceRect = self.button.bounds;
*/
[self presentViewController:vc animated:YES completion:nil];
2.3 蘋果在iOS8中對UIViewController做了類擴展
也就是說popoverPresentationController是UIViewController的屬性
modalPresentationStyle是UIViewController成員變量
UIPopoverPresentationController繼承自UIPresentationController, UIPresentationController又繼承自NSObject