获取 UIButtons 的大小(x,y,宽度,高度)

Get Size (x, y, width, height) of UIButtons

所以我正在尝试实现这个: https://github.com/aleksandrshoshiashvili/AwesomeSpotlightView

因此我想将聚光灯视图设置为按钮的实际 x、y 位置,这显然因设备而异(例如 iPhone SE 不同于 iPhone 11 Pro最大值)。

这是我试过的:

let spotlight2 = AwesomeSpotlight(
    withRect: CGRect(
        x: confirmButton.frame.origin.x,
        y: confirmButton.frame.origin.y,
        width: confirmButton.frame.width,
        height: confirmButton.frame.height
    ),
    shape: .roundRectangle,
    text: "put my text here"
)

但这只使用了我在 UIViewController... (顺便说一句:Button 会根据设备完美地改变其位置和大小)。

你有什么想法吗?

谢谢!

你应该调用 view.layoutIfNeed() 然后得到你的框架,x,y。这样它就会 return 给你正确的值