首页 > 解决方案 > 如何正确地将 WKWebView 添加到 AccessibilityContainer 中的可访问性元素?

问题描述

我有一个使用 AccessibilityContainer 和 AccessibilityElements 的自定义配置的辅助功能树。现在我想在 AccessibilityContainers 之一下添加一个 WKWebView 作为 AccessibilityElements。

一切正常,除非我想直接选择一个可访问性元素(在 Web 视图中移动手指以查找元素)。

我用已弃用的 UIWebView 替换了 WKWebView,它运行良好。

如果我没有自定义配置树,WKWebView 的可访问性也可以完美运行。

我创建了一个示例代码来重现该问题,我在此代码中所做的操作在实际应用程序中可能没有意义,但它复制了我在项目中的设置。

@implementation ViewController

- (void)viewDidLoad {
  [super viewDidLoad];

  UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 100)];
  label.backgroundColor = [UIColor yellowColor];
  label.textColor = [UIColor blueColor];
  label.text = @"Here is some text";

  CGRect webViewRect = CGRectMake(0, 100, self.view.bounds.size.width, self.view.bounds.size.height-100);

  WKWebView *webview = [[WKWebView alloc] initWithFrame:webViewRect];
  [webview loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"https://google.com"]]];
  webview.accessibilityFrame = webViewRect;

  [self.view addSubview:label];
  [self.view addSubview:webview];
  self.view.accessibilityElements = @[label, webview];
  // Do any additional setup after loading the view, typically from a nib.
}

我希望辅助功能检查器可以选择 WKWebView 中的元素,但在这种情况下它们不是。

标签: objective-cwkwebviewuiaccessibility

解决方案


推荐阅读