【问题标题】:Custom UIMenuItem not working on PDFKit's PDFView自定义 UIMenuItem 不适用于 PDFKit 的 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 没有显示

【问题讨论】:

    标签: ios objective-c ios13 ios-pdfkit apple-pdfkit


    【解决方案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]
    }
    

    【讨论】:

      猜你喜欢
      • 2021-01-31
      • 1970-01-01
      • 1970-01-01
      • 2013-03-22
      • 2021-02-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多