iOS iOS8新特性--UIPopoverPresentationController

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

原文地址:https://www.cnblogs.com/oumygade/p/4275734.html