UIAccessibility.convertToScreenCoordinates()始终返回CGRect(0,0,0,0)

时间:2019-03-15 18:59:15

标签: ios swift uiaccessibility

我试图增加自定义UIView()中按钮的点击目标,该UIView()是另一个父视图的子视图。我使用convertToScreenCoordinates()尝试实现这一目标。下面是一个例子

let rect2 = CGRect(x: 25, y: 21, width: 200, height: 44)
let newRect = UIAccessibility.convertToScreenCoordinates(rect2, in: self) 

其中self是自定义UIView()
调试中,我看到newRect = CGRect(0,0,0,0)

因此

button.accessibilityFrame = newRect // Does not work as intended as VoiceOver frame is now size 0,0 and origin 0,0

1 个答案:

答案 0 :(得分:1)

我遇到了同样的问题。原来,我是在放置视图之前设置accessibilityFrame属性。我通过将代码更改为自定义视图的layoutSubviews方法来解决此问题。

override func layoutSubviews() {
    super.layoutSubviews()

    let rect2 = CGRect(x: 25, y: 21, width: 200, height: 44)
    let newRect = UIAccessibility.convertToScreenCoordinates(rect2, in: self)
    button.accessibilityFrame = newRect
    }