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、使用

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

番薯大佬

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值