popover弹出效果,可以在当前页面中部分的显示另一个控制器,这是在大屏幕的iPad出现后,苹果为了更充分的利用屏幕做出来的一种解决方案,(这样做减少了页面跳转操作).
在7.0及以前,实现方法是使用UIPopoverController的方法,但是这种方法只能使用在iPad设备上,而不能应用到iPhone上(对于不到5寸的屏幕,也确实没有popover的必要).
直到8.0时苹果丧心病狂的出来plus(多少人被打脸,piapia的),手机尺寸达到了5.5寸.苹果随之推出了可以用在iPhone的popover解决方案,就是UIPopoverPresentationController.
UIViewController有一个属性popoverPresentationController,设置该属性可以实现iPhone上的popover效果.
示例代码
以下是要完成的效果:
效果
代码:
#import "ViewController.h"
#import "InfoViewController.h"
@interface ViewController ()<UIPopoverPresentationControllerDelegate>
@property(nonatomic,strong) InfoViewController *infoVC;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor colorWithRed:0.65 green:0.78 blue:0.65 alpha:1];
CGFloat screenW = [UIScreen mainScreen].bounds.size.width;
UIView *box = [[UIView alloc] initWithFrame:CGRectMake(0, 400, screenW, 80)];
box.backgroundColor = [UIColor colorWithRed:0.65 green:0.85 blue:0.55 alpha:1];
[self.view addSubview:box];
CGFloat margin = 10;
int buttonCount = 7;
for (int i = 0; i<buttonCount; i++) {
CGFloat buttonW = (screenW - margin)/buttonCount - margin;
CGFloat buttonX = margin + (buttonW + margin)*i;
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(buttonX, margin, buttonW, 80 - margin*2)];
[button addTarget:self action:@selector(popoverButtonClick:) forControlEvents:UIControlEventTouchUpInside];
[button setBackgroundColor:[UIColor greenColor]];
[box addSubview:button];
}
}
-(void)popoverButtonClick:(UIButton *)sender{
self.infoVC = [[InfoViewController alloc] init];
//设置弹出的样式为popover
self.infoVC.modalPresentationStyle = UIModalPresentationPopover;
//设置弹出控制器的尺寸
self.infoVC.preferredContentSize = CGSizeMake(200, 30);
//设置popoverPresentationController的sourceRect和sourceView属性
self.infoVC.popoverPresentationController.sourceRect = sender.bounds;
self.infoVC.popoverPresentationController.sourceView = sender;
//设置箭头方向
self.infoVC.popoverPresentationController.permittedArrowDirections = UIPopoverArrowDirectionDown;
//设置北京色,包括箭头
self.infoVC.popoverPresentationController.backgroundColor = [UIColor orangeColor];
self.infoVC.popoverPresentationController.delegate = self;
//弹出
[self presentViewController:self.infoVC animated:YES completion:nil];
//显示一秒后消失
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
//关闭popover
[self dismissViewControllerAnimated:YES completion:nil];
});
}
//实现该代理方法,返回UIModalPresentationNone值,可以在iPhone设备实现popover效果
-(UIModalPresentationStyle)adaptivePresentationStyleForPresentationController:(UIPresentationController *)controller{
return UIModalPresentationNone;//不适配(不区分ipad或iPhone)
}
@end
总结
popoverPresentationController是UIViewController的一个属性,在控制器vc1中用popover的样式弹出vc2时,
需要设置vc2的以下属性:
- vc2的modalPresentationStyle属性为UIModalPresentationPopover.
- vc2的preferredContentSize属性,用来确定显示大小.
- vc2的popoverPresentationController属性,包括popoverPresentationController的sourceRect, sourceView, permittedArrowDirections, backgroundColor, delegate等属性.
需要vc1实现UIPopoverPresentationControllerDelegate的代理方法,用来告诉控制器弹出时不需要判断设备类型,无论设备是iPad还是iPhone,都一popover的形式弹出:
-(UIModalPresentationStyle)adaptivePresentationStyleForPresentationController:(UIPresentationController *)controller{
//并返回UIModalPresentationNone
return UIModalPresentationNone;
}
vc1弹出vc2:
[vc1 presentViewController:vc2 animated:YES completion:nil];
vc1关闭掉弹出来的vc2:
[vc1 dismissViewControllerAnimated:YES completion:nil];
网友评论