【问题标题】:Setting the accessibilityFrame of an element whose parent view will move设置其父视图将移动的元素的accessibilityFrame
【发布时间】:2014-12-01 23:08:53
【问题描述】:

我有一个带有自定义标题的 UITableView(即我自己创建了 UIView)。我需要调整视图子视图之一的accessibilityFrame,但我不知道如何适当地设置框架的坐标——它们需要相对于窗口,但我不确定如何完成那个。

我的代码看起来像

- (UIView *)tableView:(UITableView *)tableView
viewForHeaderInSection:(NSInteger) section
{
    CGRect bounds = CGRectMake(0, 0, [tableView frame].size.width, 48);
    UIView *header = [[UIView alloc] initWithFrame:bounds];

    UILabel *labelOne = [[UILabel alloc] initWithFrame:
        CGRectMake(0, 0, bounds.size.width - 80, 18)];
    UILabel *labelTwo = [[UILabel alloc] initWithFrame:
        CGRectMake(0, 20, bounds.size.width - 80, 18)];

    CGRect frameOne = [labelOne frame];
    CGRect frameTwo = [labelTwo frame];

    [labelTwo setIsAccessibilityElement:NO];
    [labelOne setAccessibilityFrame:CGRectUnion(frameOne, frameTwo)];

    // ...

    return header;
}

我有两个 UILabel,我想将它们合并为一个以用于 VoiceOver。我通过忽略第二个标签并扩展第一个标签的框架以覆盖第二个标签的区域来实现这一点。 (第二个标签就在第一个标签的下方。)问题在于获取帧。如果我使用如上所示的代码,可访问性框架的大小是正确的,但它的位置就像 UITableView 的标题位于屏幕的左上角一样。我试着修改代码说

CGRect frameOne = [header convertRect:[labelOne frame] toView:nil];
CGRect frameTwo = [header convertRect:[labelTwo frame] toView:nil];

但同样的事情发生了。后一段代码不应该将 UILabel 的框架转换为相对于窗口的坐标吗?

我认为问题可能是当 UIView 被创建时,它不知道它在屏幕上的位置(并且作为 UITableView 的一部分,它可能会在整个地方滚动)。是否有必要将accessibilityFrame实现为每次调用时检查UIView位置的消息?

【问题讨论】:

    标签: ios accessibility


    【解决方案1】:

    有一个辅助函数可以帮助您做到这一点:UIAccessibilityConvertFrameToScreenCoordinates。此函数接受CGRect 并将其从视图的坐标系转换为屏幕坐标。

    【讨论】:

      【解决方案2】:

      我认为这不是创建 UIView 的时间,因为我认为在调用 tableView:viewForHeaderInSection:window 应该不是-nil。我认为问题在于convertRect:toView: 消息的接收者。与其将这条消息传递给header,不如将​​它传递给[self view]

      您正在从 receivers 坐标系转换为另一个视图的坐标系,在本例中为 nil 或您应用中的 UIWindow。当header 收到此消息时,您正在从header 坐标系转换为window 的坐标系,但header 本身是[self view] 的子视图。相反,您想要求[self view] 进行转换,这应该考虑到任何UINavigationBar 等。

      CGRect frameOne = [[self view] convertRect:[labelOne frame] toView:nil];
      CGRect frameTwo = [[self view] convertRect:[labelTwo frame] toView:nil];
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-12-17
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多