Search code examples
iosswiftpositionsize

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


So I'm trying to implement this: https://github.com/aleksandrshoshiashvili/AwesomeSpotlightView

Therefore I would like to set the spotlight view to the actual x,y position of my buttons, which obviously varies from device to device (e.g. iPhone SE is different from iPhone 11 Pro Max).

This is what I tried:

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"
)

But that only uses the x, y, width, height which I used at my UIViewController... (by the way: The Button changes its position and size perfectly from device to device).

Do you have any idea?

Thank you!


Solution

  • You should call view.layoutIfNeed() and then get your frame, x, y. That way it will return to you the right value