- UIPopoverPresentationController使
- UIPopoverPresentationController使
- UIPopoverPresentationController使
- iOS中UIPopoverPresentationControl
- UIPopoverController和UIPopoverPre
- UIPopoverPresentationController弹
- 官方文档告诉你UIPopoverPresentationCont
- UIPopoverPresentationController
- UIPopoverPresentationController(
- UIPopoverPresentationController

①新建一个继承自UIViewController的控制器:MSPopoverPresentationViewController
#import <UIKit/UIKit.h>
@interface MSPopoverPresentationViewController : UIViewController
// 弹出来的带箭头的视图的按钮title数组
@property (nonatomic, strong) NSMutableArray <NSString *> * dataArray;
// 通过sourceView来判断弹出来的带箭头的视图在什么位置
@property (nonatomic, strong) UIView *sourceView;
// 弹出来的带箭头的视图的背景颜色
@property (nonatomic, strong) UIColor *backgroundColor;
// 弹出来的带箭头的视图的按钮字体颜色
@property (nonatomic, strong) UIColor *textColor;
// 弹出来的带箭头的视图的箭头的方向
@property (nonatomic, assign) UIPopoverArrowDirection permittedArrowDirections;
// 弹出来的带箭头的视图的按钮点击事件
- (void)addClickBlock:(void (^) (NSInteger index))handle;
@end
#import "MSPopoverPresentationViewController.h"
#import <objc/runtime.h>
@interface MSPopoverPresentationViewController () <UITableViewDataSource, UITableViewDelegate, UIPopoverPresentationControllerDelegate>
@property (strong, nonatomic, nullable) UITableView *tableView;
@end
@implementation MSPopoverPresentationViewController
static NSString * cellIdentifier = @"POPCELL";
static char MSTableViewBlock;
- (instancetype)init {
if (self = [super init]) {
// 这两行代码先后顺序不能颠倒
self.modalPresentationStyle = UIModalPresentationPopover;
self.popoverPresentationController.delegate = self;
}
return self;
}
- (void)viewDidLoad {
[super viewDidLoad];
self.tableView = [[UITableView alloc] init];
self.tableView.frame = CGRectMake(0, 0, self.preferredContentSize.width, self.preferredContentSize.height);
self.tableView.separatorInset = UIEdgeInsetsZero;
self.tableView.delegate = self;
self.tableView.dataSource = self;
self.tableView.scrollEnabled = NO;
[self.view addSubview:self.tableView];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return self.dataArray.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return tableView.frame.size.height / self.dataArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:(UITableViewCellStyleDefault) reuseIdentifier:cellIdentifier];
}
cell.textLabel.text = self.dataArray[indexPath.row];
if (self.textColor) cell.textLabel.textColor = self.textColor;
if (self.backgroundColor) cell.backgroundColor = self.backgroundColor;
cell.textLabel.textAlignment = NSTextAlignmentCenter;
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[tableView deselectRowAtIndexPath:indexPath animated:YES];
[self dismissViewControllerAnimated:NO completion:nil];
void (^ handle)(NSInteger) = objc_getAssociatedObject(self, &MSTableViewBlock);
if (handle) {
handle(indexPath.row);
}
}
- (void)addClickBlock:(void (^)(NSInteger))handle {
objc_setAssociatedObject(self, &MSTableViewBlock, handle, OBJC_ASSOCIATION_COPY_NONATOMIC);
}
- (void)setDataArray:(NSMutableArray *)dataArray {
_dataArray = dataArray;
[self.tableView reloadData];
}
- (void)setSourceView:(UIView *)sourceView {
self.popoverPresentationController.sourceView = sourceView;
self.popoverPresentationController.sourceRect = sourceView.bounds;
}
- (void)setPermittedArrowDirections:(UIPopoverArrowDirection)permittedArrowDirections {
self.popoverPresentationController.permittedArrowDirections = permittedArrowDirections;
}
- (void)setBackgroundColor:(UIColor *)backgroundColor {
_backgroundColor = backgroundColor;
self.popoverPresentationController.backgroundColor = backgroundColor;
}
- (UIModalPresentationStyle)adaptivePresentationStyleForPresentationController:(UIPresentationController *)controller {
return UIModalPresentationNone;
}
@end
②使用MSPopoverPresentationViewController
#import "ViewController.h"
#import "MSPopoverPresentationViewController.h"
@interface ViewController ()
@property (nonatomic, strong) NSMutableArray *dataArray;
@end
@implementation ViewController
static CGFloat rowHeights = 50.0;
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
self.dataArray = [NSMutableArray arrayWithObjects:@"1245454", @"254fdsa", @"大范甘迪的", @"5tg4fd53", nil];
}
- (IBAction)buttonAction:(id)sender {
MSPopoverPresentationViewController *ppVC = [MSPopoverPresentationViewController new];
ppVC.dataArray = self.dataArray;
ppVC.sourceView = sender;
ppVC.textColor = [UIColor darkGrayColor];
ppVC.permittedArrowDirections = UIPopoverArrowDirectionUp;
ppVC.backgroundColor = [UIColor whiteColor];
ppVC.preferredContentSize = CGSizeMake(100, rowHeights * self.dataArray.count);
[self presentViewController:ppVC animated:NO completion:nil];
__weak typeof(self) weakSelf = self;
[ppVC addClickBlock:^(NSInteger index) {
NSLog(@"%@", weakSelf.dataArray[index]);
}];
}
@end
③注意事项:从效果图不难看出,文字过长会显示不全,这个需要配合ppVC.preferredContentSize = CGSizeMake(100, rowHeights * self.dataArray.count);
使用,说明100的宽度不够,需要适当加宽。
网友评论