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];