Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get a View's Width (Swift)

Tags:

ios

swift

uiview

So you can get the width and height of the UIScreen as follows...

public var screenWidth: CGFloat {
    return UIScreen.main.bounds.width
}

public var screenHeight: CGFloat {
    return UIScreen.main.bounds.height
}

But how can you get the width and height of a UIView?

like image 477
Ben Avatar asked Jul 12 '26 16:07

Ben


1 Answers

extension UIView {
    public var viewWidth: CGFloat {
        return self.frame.size.width
    }

    public var viewHeight: CGFloat {
        return self.frame.size.height
    }
}
like image 154
qtngo Avatar answered Jul 15 '26 04:07

qtngo