1.相关属性
- sourceRect:指定箭头所指区域的矩形框范围,以sourceview的左上角为坐标原点
- permittedArrowDirections:箭头方向
- sourceView:sourceRect以这个view的左上角为原点
- barButtonItem:若有navigationController,并且从right/leftBarButtonItem点击后出现popover,则可以把right/leftBarButtonItem看做上面说的sourceView.默认箭头指向up,亲测下来up是最合适的方向,所以在这种情况下可以不设置箭头方向。
UIPopoverPresentationController是UIViewController的一个属性,所以并不需要你特地去建立
一个UIPopoverPresentationController来进行操作,而应该建立一个UIViewController。
2. 效果图
3. 代码实现
- ViewController中的实现:
#import "ViewController.h"
#import "PopoverViewController.h"
@interface ViewController ()
@property (strong, nonatomic) UIButton *button;
@property (strong, nonatomic) PopoverViewController *buttonPopVC;
@property (strong, nonatomic) PopoverViewController *itemPopVC;
@property (strong, nonatomic) NSString *currentPop;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"item" style:UIBarButtonItemStylePlain target:self action:@selector(rightItemClick)];
self.view.backgroundColor = [UIColor whiteColor];
_button = [[UIButton alloc] initWithFrame:CGRectMake(20, 100, 100, 40)];
[_button setTitle:@"button" forState:UIControlStateNormal];
[_button setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
[self.view addSubview:_button];
[_button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(tableDidSelected:) name:@"click" object:nil];
}
- (void)rightItemClick{
self.itemPopVC = [[PopoverViewController alloc] init];
self.itemPopVC.modalPresentationStyle = UIModalPresentationPopover;
self.itemPopVC.popoverPresentationController.barButtonItem = self.navigationItem.rightBarButtonItem; //rect参数是以view的左上角为坐标原点(0,0)
self.itemPopVC.popoverPresentationController.permittedArrowDirections = UIPopoverArrowDirectionUnknown; //箭头方向,如果是baritem不设置方向,会默认up,up的效果也是最理想的
self.itemPopVC.popoverPresentationController.delegate = self;
[self presentViewController:self.itemPopVC animated:YES completion:nil];
}
//处理popover上的talbe的cell点击
- (void)tableDidSelected:(NSNotification *)notification {
NSIndexPath *indexpath = (NSIndexPath *)notification.object;
switch (indexpath.row) {
case 0:
self.view.backgroundColor = [UIColor greenColor];
break;
case 1:
self.view.backgroundColor = [UIColor grayColor];
break;
case 2:
self.view.backgroundColor = [UIColor blueColor];
break;
case 3:
self.view.backgroundColor = [UIColor purpleColor];
break;
case 4:
self.view.backgroundColor = [UIColor yellowColor];
break;
}
if (self.buttonPopVC) {
[self.buttonPopVC dismissViewControllerAnimated:YES completion:nil]; //我暂时使用这个方法让popover消失,但我觉得应该有更好的方法,因为这个方法并不会调用popover消失的时候会执行的回调。
self.buttonPopVC = nil;
}else{
[self.itemPopVC dismissViewControllerAnimated:YES completion:nil];
self.itemPopVC = nil;
}
}
- (void)buttonClick:(UIButton *)sender{
self.buttonPopVC = [[PopoverViewController alloc] init];
self.buttonPopVC.modalPresentationStyle = UIModalPresentationPopover;
self.buttonPopVC.popoverPresentationController.sourceView = _button; //rect参数是以view的左上角为坐标原点(0,0)
self.buttonPopVC.popoverPresentationController.sourceRect = _button.bounds; //指定箭头所指区域的矩形框范围(位置和尺寸),以view的左上角为坐标原点
self.buttonPopVC.popoverPresentationController.permittedArrowDirections = UIPopoverArrowDirectionUp; //箭头方向
self.buttonPopVC.popoverPresentationController.delegate = self;
[self presentViewController:self.buttonPopVC animated:YES completion:nil];
}
- (UIModalPresentationStyle)adaptivePresentationStyleForPresentationController:(UIPresentationController *)controller{
return UIModalPresentationNone;
}
- (BOOL)popoverPresentationControllerShouldDismissPopover:(UIPopoverPresentationController *)popoverPresentationController{
return NO; //点击蒙版popover不消失, 默认yes
}
@end
- 自定义类
@interface PopoverViewController : UIViewController
@property (strong, nonatomic) UITableView *tableView;
@property (strong, nonatomic) NSMutableArray *colorArray;
@end
#import "PopoverViewController.h"
@implementation PopoverViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.tableView = [[UITableView alloc] initWithFrame:self.view.frame];
[self.view addSubview:self.tableView];
self.tableView.dataSource = self;
self.tableView.delegate = self;
self.tableView.scrollEnabled = NO;
self.colorArray = [[NSMutableArray alloc] initWithObjects:@"green",@"gray", @"blue",@"purple", @"yellow", nil];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return self.colorArray.count;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *identifer = @"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifer];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifer];
}
cell.textLabel.text = [NSString stringWithFormat:@"%@", self.colorArray[indexPath.row]];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
[[NSNotificationCenter defaultCenter] postNotificationName:@"click" object:indexPath];
}
//重写preferredContentSize,让popover返回你期望的大小
- (CGSize)preferredContentSize {
if (self.presentingViewController && self.tableView != nil) {
CGSize tempSize = self.presentingViewController.view.bounds.size;
tempSize.width = 150;
CGSize size = [self.tableView sizeThatFits:tempSize]; //sizeThatFits返回的是最合适的尺寸,但不会改变控件的大小
return size;
}else {
return [super preferredContentSize];
}
}
- (void)setPreferredContentSize:(CGSize)preferredContentSize{
super.preferredContentSize = preferredContentSize;
}
@end