iPad开发中UIPopoverPresentationController的使用

效果图
![UIPopoverPresentationController使用(https://img-blog.csdn.net/20180801112016614?watermark/2/text/aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3BvdGF0bzUxMg==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70)

UIPopoverPresentationController使用说明
1、创建弹窗视图控制器,如:MenuController
2、实例化弹窗视图控制器时,设置其显示大小preferredContentSize和显示模式modalPresentationStyle
3、根据实例化的弹窗视图控制器获取视图控制器UIPopoverPresentationController
4、设置UIPopoverPresentationController的相关属性,如:箭头显示方向permittedArrowDirections、显示视图sourceView、显示位置sourceRect、显示时的背景颜色backgroundColor
5、根据需要设置代理delegate,及实现代理协议UIPopoverPresentationControllerDelegate方法
6、显示,如:[self presentViewController:self.menuVC animated:YES completion:nil];
7、隐藏,如:[self dismissViewControllerAnimated:YES completion:NULL];

使用注意事项:设置显示位置sourceRect和显示视图sourceView时,通常是根据子视图的来做处理的。如,要显示在按钮button的旁边,则sourceView为button,且sourceRect为button的button.bounds;或sourceView为button的父视图view,且sourceRect为buttond在父视图view的位置button.frame

代码示例
1、继承UIViewController创建视图控制器MenuViewController

#import <UIKit/UIKit.h>

@interface MenuViewController : UIViewController

@property (nonatomic, copy) void (^itemClick)(NSInteger index);

@end
#import "MenuViewController.h"

@interface MenuViewController () <UITableViewDelegate, UITableViewDataSource>

@end

@implementation MenuViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    UITableView *tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    [self.view addSubview:tableView];
    tableView.delegate = self;
    tableView.dataSource = self;
    [tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"UITableViewCell"];
    tableView.tableFooterView = [UIView new];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 30;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"UITableViewCell"];
    cell.textLabel.text = [NSString stringWithFormat:@"%@", @(indexPath.row)];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];

    if (self.itemClick) {
        self.itemClick(indexPath.row);
    }
}

@end

2、使用

//
//  ViewController.m
//  demo
//
//  Created by zhangshaoyu on 2018/7/30.
//  Copyright © 2018年 zhangshaoyu. All rights reserved.
//

#import "ViewController.h"
#import "MenuViewController.h"

@interface ViewController () <UIPopoverPresentationControllerDelegate>

@property (nonatomic, strong) MenuViewController *menuVC;

@property (nonatomic, strong) UIButton *button;
@property (nonatomic, strong) UIButton *button1;
@property (nonatomic, strong) UIButton *button2;
@property (nonatomic, strong) UIButton *button3;
@property (nonatomic, strong) UIButton *button4;
@property (nonatomic, strong) UIButton *button5;
@property (nonatomic, strong) UIButton *button6;
@property (nonatomic, strong) UIButton *button7;
@property (nonatomic, strong) UIButton *button8;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    self.navigationItem.title = @"测试";

    //
    self.button = [[UIButton alloc] initWithFrame:CGRectMake(10.0, 10.0, 80.0, 30.0)];
    [self.view addSubview:self.button];
    [self.button setTitle:@"级别0" forState:UIControlStateNormal];
    [self.button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [self.button setTitleColor:[UIColor redColor] forState:UIControlStateHighlighted];
    [self.button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
    //
    self.button1 = [[UIButton alloc] initWithFrame:CGRectMake((self.view.frame.size.width - 80.0) / 2, 10.0, 80.0, 30.0)];
    [self.view addSubview:self.button1];
    [self.button1 setTitle:@"级别1" forState:UIControlStateNormal];
    [self.button1 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [self.button1 setTitleColor:[UIColor redColor] forState:UIControlStateHighlighted];
    [self.button1 addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
    //
    self.button2 = [[UIButton alloc] initWithFrame:CGRectMake((self.view.frame.size.width - 10.0 - 80.0), 10.0, 80.0, 30.0)];
    [self.view addSubview:self.button2];
    [self.button2 setTitle:@"级别2" forState:UIControlStateNormal];
    [self.button2 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [self.button2 setTitleColor:[UIColor redColor] forState:UIControlStateHighlighted];
    [self.button2 addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
    //
    self.button3 = [[UIButton alloc] initWithFrame:CGRectMake(10.0, (self.view.frame.size.height - 30.0) / 2, 80.0, 30.0)];
    [self.view addSubview:self.button3];
    [self.button3 setTitle:@"级别3" forState:UIControlStateNormal];
    [self.button3 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [self.button3 setTitleColor:[UIColor redColor] forState:UIControlStateHighlighted];
    [self.button3 addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
    //
    self.button4 = [[UIButton alloc] initWithFrame:CGRectMake((self.view.frame.size.width - 80.0) / 2, (self.view.frame.size.height - 30.0) / 2, 80.0, 30.0)];
    [self.view addSubview:self.button4];
    [self.button4 setTitle:@"级别4" forState:UIControlStateNormal];
    [self.button4 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [self.button4 setTitleColor:[UIColor redColor] forState:UIControlStateHighlighted];
    [self.button4 addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
    //
    self.button5 = [[UIButton alloc] initWithFrame:CGRectMake((self.view.frame.size.width - 10.0 - 80.0), (self.view.frame.size.height - 30.0) / 2, 80.0, 30.0)];
    [self.view addSubview:self.button5];
    [self.button5 setTitle:@"级别5" forState:UIControlStateNormal];
    [self.button5 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [self.button5 setTitleColor:[UIColor redColor] forState:UIControlStateHighlighted];
    [self.button5 addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
    //
    self.button6 = [[UIButton alloc] initWithFrame:CGRectMake(10.0, (self.view.frame.size.height - 10.0 - 30.0), 80.0, 30.0)];
    [self.view addSubview:self.button6];
    self.button6.autoresizingMask = UIViewAutoresizingFlexibleTopMargin;
    [self.button6 setTitle:@"级别6" forState:UIControlStateNormal];
    [self.button6 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [self.button6 setTitleColor:[UIColor redColor] forState:UIControlStateHighlighted];
    [self.button6 addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
    //
    self.button7 = [[UIButton alloc] initWithFrame:CGRectMake((self.view.frame.size.width - 80.0) / 2, (self.view.frame.size.height - 10.0 - 30.0), 80.0, 30.0)];
    [self.view addSubview:self.button7];
    self.button7.autoresizingMask = UIViewAutoresizingFlexibleTopMargin;
    [self.button7 setTitle:@"级别7" forState:UIControlStateNormal];
    [self.button7 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [self.button7 setTitleColor:[UIColor redColor] forState:UIControlStateHighlighted];
    [self.button7 addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
    //
    self.button8 = [[UIButton alloc] initWithFrame:CGRectMake((self.view.frame.size.width - 10.0 - 80.0), (self.view.frame.size.height - 10.0 - 30.0), 80.0, 30.0)];
    [self.view addSubview:self.button8];
    self.button8.autoresizingMask = UIViewAutoresizingFlexibleTopMargin;
    [self.button8 setTitle:@"级别8" forState:UIControlStateNormal];
    [self.button8 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [self.button8 setTitleColor:[UIColor redColor] forState:UIControlStateHighlighted];
    [self.button8 addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)loadView
{
    [super loadView];
    if ([self respondsToSelector:@selector(setEdgesForExtendedLayout:)]) {
        [self setEdgesForExtendedLayout:UIRectEdgeNone];
    }
}

// 显示弹窗
- (void)buttonClick:(UIButton *)button
{
    UIPopoverPresentationController *popover = [self.menuVC popoverPresentationController];
    popover.delegate = self;
    popover.permittedArrowDirections = UIPopoverArrowDirectionUp;//设置箭头位置
    // 1方法
//    popover.sourceView = self.view;//设置目标视图
//    popover.sourceRect = button.frame;//弹出视图显示位置
    // 2方法
    popover.sourceView = button;//设置目标视图
    popover.sourceRect = button.bounds;//弹出视图显示位置
    //
    popover.backgroundColor = [UIColor whiteColor];//设置弹窗背景颜色
    //
    [self presentViewController:self.menuVC animated:YES completion:nil];
}

// 菜单视图选择并隐藏弹窗
- (void)itemClick:(NSInteger)index
{
    NSLog(@"index = %@", @(index + 1));
    [self dismissViewControllerAnimated:YES completion:NULL];
}

- (MenuViewController *)menuVC
{
    if (_menuVC == nil) {
        _menuVC = [MenuViewController new];
        //
        ViewController __weak *weakSelf = self;
        _menuVC.itemClick = ^(NSInteger index) {
            [weakSelf itemClick:index];
        };
        //
        _menuVC.preferredContentSize = CGSizeMake(300, 200);// 设置浮窗的宽高
        _menuVC.modalPresentationStyle = UIModalPresentationPopover;
    }
    return _menuVC;
}

#pragma mark - 代理方法

// 设置 浮窗弹窗的推出样式,效果图中设置style为UIModalPresentationNone
- (UIModalPresentationStyle)adaptivePresentationStyleForPresentationController:(UIPresentationController *)controller
{
    NSLog(@"1 %s", __func__);
    return UIModalPresentationPopover;
}

// 点击浮窗背景popover controller是否消失
- (BOOL)popoverPresentationControllerShouldDismissPopover:(UIPopoverPresentationController *)popoverPresentationController
{
    NSLog(@"2 %s", __func__);
    return YES;
}

// 浮窗消失时调用
- (void)popoverPresentationControllerDidDismissPopover:(UIPopoverPresentationController *)popoverPresentationController
{
    NSLog(@"3 %s", __func__);
}

@end

猜你喜欢

转载自blog.csdn.net/potato512/article/details/81327711
今日推荐