UIPopoverPresentationController簡(jiǎn)單使用,實(shí)現(xiàn)popover

1.相關(guān)屬性

  1. sourceRect:指定箭頭所指區(qū)域的矩形框范圍,以sourceview的左上角為坐標(biāo)原點(diǎn)
  2. permittedArrowDirections:箭頭方向
  3. sourceView:sourceRect以這個(gè)view的左上角為原點(diǎn)
  4. 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. 效果圖

系統(tǒng)的.png

3. 代碼實(shí)現(xiàn)

  1. 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

  1. 自定義類(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)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容