这次写这个UIPopoverPresentationController,是因为这段时间在用UIPopoverController,可是发现他会警告说这个已经过时了,请使用UIPopoverPresentationController。写的特别简单,但是肯定已经够大家用了... ...
本来想把具体属性写出来的,发现还不如直接用代码打给大家看... ...
//这里是我直接用storyboard拖的一个button方法
- (IBAction)add:(id)sender {
//用UIViewController或者自定义(这里我使用的是自定义的) - - 这里一般用自定义的,好添加控件,好操作啊
NewViewController *vc = [NewViewControllernew];
//这里必须设成UIModalPresentationPopover的形式,要不然大小是不好控制,特别是对于iPhone来说
vc.modalPresentationStyle =UIModalPresentationPopover;
//设置源视图,这里建议直接设成button本身就行
vc.popoverPresentationController.sourceView = sender;
//这里是设置你希望展示的大小
vc.preferredContentSize =CGSizeMake(100,100);
/*这里是展示的位置,他是相对于你前面设置的sourceView...PS:苹果开发文档说这里是设置相对位置和大小的,其实只能设置位置,后面的两位数没什么luan用
注:这里的坐标是相对于button(也就是sourceview)的左顶点,你可以把button想像成他的父视图也行
*/
vc.popoverPresentationController.sourceRect =CGRectMake(20,0, 0,0);//这里我的button是40×40的,(20,0)就是button的上面居中位置,就是下面图片那个白色突出的尖(呵呵呵... ...)
/*
UIPopoverArrowDirectionUp = 1UL << 0,向上
UIPopoverArrowDirectionDown = 1UL << 1,向下
UIPopoverArrowDirectionLeft = 1UL << 2,向左
UIPopoverArrowDirectionRight = 1UL << 3,向右
UIPopoverArrowDirectionAny = UIPopoverArrowDirectionUp | UIPopoverArrowDirectionDown | UIPopoverArrowDirectionLeft | UIPopoverArrowDirectionRight, 根据button相对view的位置自动选择
UIPopoverArrowDirectionUnknown = NSUIntegerMax还不知道
*/
//箭头指向
[vc.popoverPresentationControllersetPermittedArrowDirections:UIPopoverArrowDirectionUp];
//设置代理(一定要设)
vc.popoverPresentationController.delegate =self;
//展示出来
[selfpresentViewController:vc animated:YEScompletion:nil];
}
下面的图片:红色就是sourceview(button),蓝色就是弹出来的控制器,里面那个橘色的我是测试用的一个button