1.相關(guān)屬性
- sourceRect:指定箭頭所指區(qū)域的矩形框范圍蜂嗽,以sourceview的左上角為坐標(biāo)原點(diǎn)
- permittedArrowDirections:箭頭方向
- sourceView:sourceRect以這個(gè)view的左上角為原點(diǎn)
- barButtonItem:若有navigationController,并且從right/leftBarButtonItem點(diǎn)擊后出現(xiàn)popover,則可以把right/leftBarButtonItem看做上面說(shuō)的sourceView.默認(rèn)箭頭指向up,親測(cè)下來(lái)up是最合適的方向殃恒,所以在這種情況下可以不設(shè)置箭頭方向植旧。
UIPopoverPresentationController是UIViewController的一個(gè)屬性,所以并不需要你特地去建立
一個(gè)UIPopoverPresentationController來(lái)進(jìn)行操作离唐,而應(yīng)該建立一個(gè)UIViewController病附。
2. 效果圖
3. 代碼實(shí)現(xiàn)
- ViewController中的實(shí)現(xiàn):
#import "ViewController.h"
#import "PopoverViewController.h"
@interface ViewController ()<UIPopoverPresentationControllerDelegate>
@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參數(shù)是以view的左上角為坐標(biāo)原點(diǎn)(0,0)
self.itemPopVC.popoverPresentationController.permittedArrowDirections = UIPopoverArrowDirectionUnknown; //箭頭方向,如果是baritem不設(shè)置方向亥鬓,會(huì)默認(rèn)up完沪,up的效果也是最理想的
self.itemPopVC.popoverPresentationController.delegate = self;
[self presentViewController:self.itemPopVC animated:YES completion:nil];
}
//處理popover上的talbe的cell點(diǎn)擊
- (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]; //我暫時(shí)使用這個(gè)方法讓popover消失,但我覺(jué)得應(yīng)該有更好的方法嵌戈,因?yàn)檫@個(gè)方法并不會(huì)調(diào)用popover消失的時(shí)候會(huì)執(zhí)行的回調(diào)覆积。
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參數(shù)是以view的左上角為坐標(biāo)原點(diǎn)(0,0)
self.buttonPopVC.popoverPresentationController.sourceRect = _button.bounds; //指定箭頭所指區(qū)域的矩形框范圍(位置和尺寸)熟呛,以view的左上角為坐標(biāo)原點(diǎn)
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; //點(diǎn)擊蒙版popover不消失宽档, 默認(rèn)yes
}
@end
- 自定義類(lèi)
@interface PopoverViewController : UIViewController<UITableViewDataSource, UITableViewDelegate>
@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];
}
//重寫(xiě)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返回的是最合適的尺寸惰拱,但不會(huì)改變控件的大小
return size;
}else {
return [super preferredContentSize];
}
}
- (void)setPreferredContentSize:(CGSize)preferredContentSize{
super.preferredContentSize = preferredContentSize;
}
@end
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者