自定义 UIMenuItem 不适用于 PDFKit 的 PDFView
Posted
技术标签:
【中文标题】自定义 UIMenuItem 不适用于 PDFKit 的 PDFView【英文标题】:Custom UIMenuItem not working on PDFKit's PDFView 【发布时间】:2019-12-21 08:34:24 【问题描述】:我正在尝试将自定义 UIMenuItem
添加到我的 PDFView
这是我在示例 Xcode 项目中所做的事情
#import <PDFKit/PDFKit.h>
#import <objc/runtime.h>
#import "ViewController.h"
@interface ViewController ()
@property(nonatomic) PDFDocument *document;
@property(nonatomic) PDFView *pdfView;
@end
@implementation ViewController
- (void)viewDidLoad
[super viewDidLoad];
// Do any additional setup after loading the view.
NSURL *pdfPath = [[NSBundle mainBundle] URLForResource:@"pdf" withExtension:@"pdf"];
_document = [[PDFDocument alloc] initWithURL:pdfPath];
_pdfView = [[PDFView alloc] initWithFrame:CGRectZero];
_pdfView.document = _document;
[self.view addSubview:_pdfView];
_pdfView.translatesAutoresizingMaskIntoConstraints = NO;
[_pdfView.topAnchor constraintEqualToAnchor:self.view.topAnchor].active = YES;
[_pdfView.bottomAnchor constraintEqualToAnchor:self.view.bottomAnchor].active = YES;
[_pdfView.leadingAnchor constraintEqualToAnchor:self.view.leadingAnchor].active = YES;
[_pdfView.trailingAnchor constraintEqualToAnchor:self.view.trailingAnchor].active = YES;
UIMenuItem *menuItem =
[[UIMenuItem alloc] initWithTitle:@"Custom Action"
action:@selector(doSomething)];
[[UIMenuController sharedMenuController] setMenuItems:@[ menuItem ]];
[[UIMenuController sharedMenuController] update];
- (BOOL)canPerformAction:(SEL)action withSender:(id)sender
if (sel_isEqual(action, @selector(doSomething)))
return YES;
return NO;
- (void)viewDidAppear:(BOOL)animated
[self becomeFirstResponder];
- (void)doSomething
NSLog(@"In Do Something!");
- (BOOL)canBecomeFirstResponder
return YES;
@end
这在 ios 11 和 12 上运行良好,但在 iOS 13 上,UIMenuItem
没有显示
【问题讨论】:
【参考方案1】:对我来说,在 NSNotification.Name.PDFViewSelectionChanged
的处理程序中分配 menuItems
有效
NotificationCenter.default.addObserver(self,
selector: #selector(selectionChangeNotification),
name: NSNotification.Name.PDFViewSelectionChanged,
object: pdfView)
@objc
private func selectionChangeNotification()
let menuItem = UIMenuItem(title: "Custom Action", action: #selector(doSomething))
UIMenuController.shared.menuItems = [menuItem]
【讨论】:
以上是关于自定义 UIMenuItem 不适用于 PDFKit 的 PDFView的主要内容,如果未能解决你的问题,请参考以下文章
如何为 UITableViewCell 显示自定义 UIMenuItem?
UIMenuContoller UIMenuItem 具有多个参数的自定义操作?
ios 7 - 自定义 UIMenuItem 在 TableViewCell 上不起作用