UIPopoverPresentationController
2017-05-12 本文已影响0人
merlinCry
这个类是用来做弹出窗口的,原本是ipad专用到iOS8之后iPhone也可以用了。用他可以轻松实现类似微信右上角加号那个弹窗。
这个类是UIPresentationController的子类。UIPresentationController是iOS8之后开放,专门用来persent一个viewController使用的。在modal转场中用来做containerView。
下面用它实现一个右上角弹出菜单。当然不只是菜单,他可以弹出任何视图,以为他弹出来的是个UIViewController,vc中可以放任何视图,来构建你的弹出窗口.
实现步骤
不需要去创建UIPopoverPresentationController类,因为可以从UIViewController的popoverPresentationController属性获取他。实现弹出窗口时,只需要配置被弹出VC的popoverPresentationController属性,然后将其present出来就可。
核心代码
//配置被弹出vc
//popMenuVC是将要以窗口形式弹出的vc
self.popMenuVC.modalPresentationStyle = UIModalPresentationPopover;
//箭头方向
self.popMenuVC.popoverPresentationController.permittedArrowDirections = UIPopoverArrowDirectionAny;
//箭头颜色
self.popMenuVC.popoverPresentationController.backgroundColor = [UIColor whiteColor];
//代理必须设置
self.popMenuVC.popoverPresentationController.delegate = self;
//窗口size
self.popMenuVC.preferredContentSize = CGSizeMake(120, 160);
//箭头指向的视图
self.popMenuVC.popoverPresentationController.sourceView = btn;
//这里只需要箭头指向视图的size可以通过他来调整箭头与其指向视图的距离
self.popMenuVC.popoverPresentationController.sourceRect = btn.bounds;
//可以指向navigation的barButtonitem
// self.popMenuVC.popoverPresentationController.barButtonItem = self.navigationItem.rightBarButtonItem;
delmo
#import "ViewController.h"
#import "EDPopMenuVC.h"
@interface ViewController ()<UIPopoverPresentationControllerDelegate>
{
UIButton *btn;
}
@property(nonatomic,strong)EDPopMenuVC *popMenuVC;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self popMenuVC];
self.view.backgroundColor = [UIColor orangeColor];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(rightBarAction:)];
btn = [[UIButton alloc]initWithFrame:CGRectMake(150, 150, 40, 40)];
btn.backgroundColor = [UIColor greenColor];
[btn addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn];
}
-(void)popConfig
{
self.popMenuVC.modalPresentationStyle = UIModalPresentationPopover;
self.popMenuVC.popoverPresentationController.permittedArrowDirections = UIPopoverArrowDirectionAny;
self.popMenuVC.popoverPresentationController.backgroundColor = [UIColor whiteColor];
self.popMenuVC.popoverPresentationController.delegate = self;
self.popMenuVC.preferredContentSize = CGSizeMake(120, 160);
}
-(EDPopMenuVC *)popMenuVC
{
if (!_popMenuVC) {
_popMenuVC = [EDPopMenuVC new];
}
return _popMenuVC;
}
-(void)buttonAction:(UIButton *)sender
{
[self popConfig];
self.popMenuVC.popoverPresentationController.sourceView = btn;
self.popMenuVC.popoverPresentationController.sourceRect = btn.bounds;
[self presentViewController:self.popMenuVC animated:YES completion:nil];
}
-(void)rightBarAction:(UIBarButtonItem *)item
{
[self popConfig];
self.popMenuVC.popoverPresentationController.barButtonItem = self.navigationItem.rightBarButtonItem;
[self presentViewController:self.popMenuVC animated:YES completion:nil];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
//不要用这个方法,用下面那个(这个在plus上横屏的时候样式有问题)
//-(UIModalPresentationStyle)adaptivePresentationStyleForPresentationController:(UIPresentationController //*)controller
//{
// return UIModalPresentationNone;
//}
- (UIModalPresentationStyle)adaptivePresentationStyleForPresentationController:(UIPresentationController *)controller traitCollection:(UITraitCollection *)traitCollection
{
return UIModalPresentationNone;
}
@end
//被弹出VC
#import "EDPopMenuVC.h"
@interface EDPopMenuVC ()<UITableViewDelegate,UITableViewDataSource>
@property(strong,nonatomic)UITableView *contentTable;
@property(strong,nonatomic)NSArray *dataSource;
@end
@implementation EDPopMenuVC
- (void)viewDidLoad {
[super viewDidLoad];
[self createSubView];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(void)createSubView
{
_dataSource = @[@"扫一扫",@"加好友",@"创建讨论组",@"收钱",@"付钱"];
_contentTable = [[UITableView alloc]initWithFrame:self.view.frame style:UITableViewStylePlain];
_contentTable.separatorStyle = UITableViewCellSeparatorStyleNone;
_contentTable.delegate = self;
_contentTable.dataSource = self;
[self.view addSubview:_contentTable];
}
#pragma mark -----
#pragma mark --------UITableViewDelegate && UITableViewDataSource--------
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return _dataSource.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 30;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *CellIdentifier = [NSString stringWithFormat:@"%@ident",NSStringFromClass(self.class)];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
cell.selectionStyle = 0;
}
cell.textLabel.text = _dataSource[indexPath.row];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:NO];
NSLog(@"%@",_dataSource[indexPath.row]);
}
@end