我试图增加自定义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
我遇到了同样的问题。原来我在定位视图之前设置了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
}