[英]Custom UIMenuItem not working on PDFKit's PDFView
我正在尝试将自定义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
没有显示
对我来说,在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]
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.