gpt4 book ai didi

ios - 自定义 UIMenuItem 不适用于 PDFKit 的 PDFView

转载 作者:行者123 更新时间:2023-12-03 11:16:54 25 4
gpt4 key购买 nike

我正在尝试添加自定义 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没有出现

最佳答案

为我分配 menuItemsNSNotification.Name.PDFViewSelectionChanged 的处理程序中作品

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]
}

关于ios - 自定义 UIMenuItem 不适用于 PDFKit 的 PDFView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57500560/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com