码迷,mamicode.com
首页 > 其他好文 > 详细

iphone 与 ipad -- UIPopoverPresentationViewController

时间:2015-12-11 11:21:37      阅读:274      评论:0      收藏:0      [点我收藏+]

标签:

  iOS8.0之后, 苹果推出了UIPopoverPresentationViewController, 在弹出控制器时, 统一采用 presentViewController,

但是要实现iPhone和iPad能够统一使用一段代码, 需要进行一些设置:

 

@implementation ViewController

// UIPopoverController只能用在iPad

- (void)viewDidLoad {

    [super viewDidLoad];

    // Do any additional setup after loading the view, typically fro

}

 

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {

    HMSecondViewController *vc = [[HMSecondViewController alloc] init];

    // modal出来是个popover

    vc.modalPresentationStyle = UIModalPresentationPopover;

    // 取出vc所在的UIPopoverPresentationController

    vc.popoverPresentationController.sourceView = self.slider;

    vc.popoverPresentationController.sourceRect = self.slider.bounds;

    [self presentViewController:vc animated:YES completion:nil];

    }

@end

例如: UIAlertController

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {    

    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"警告" message:@"你有严重的精神病,赶紧去治疗" preferredStyle:UIAlertControllerStyleActionSheet];

    // 默认是POPover

    alert.modalPresentationStyle = UIModalPresentationPopover;

    // 设置popover指向的item

//    alert.popoverPresentationController.barButtonItem = self.navigationItem.leftBarButtonItem;

    alert.popoverPresentationController.sourceView = _clickBtn;

    alert.popoverPresentationController.sourceRect = _clickBtn.bounds;

    // 添加按钮

    [alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {

        NSLog(@"点击了确定按钮");

    }]];

    [alert addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {

        NSLog(@"点击了取消按钮");

    }]];

    

    [self presentViewController:alert animated:YES completion:nil];

}

 

iphone 与 ipad -- UIPopoverPresentationViewController

标签:

原文地址:http://www.cnblogs.com/guangleijia/p/5038391.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!