Ben
Ben

Reputation: 3804

Get a View's Width (Swift)

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?

Upvotes: 6

Views: 14086

Answers (1)

qtngo
qtngo

Reputation: 1679

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

    public var viewHeight: CGFloat {
        return self.frame.size.height
    }
}

Upvotes: 18

Related Questions