fuzzygoat fuzzygoat - 1 year ago 68
iOS Question

Accessing content scaleFactor / multiplier?

I am working on an iPhone 6 Plus, and getting the bounds of my UIView using:

let viewBounds = view.bounds // GIVES 736 x 414 (Points), WHICH IS RIGHT

Is there any way that I can get the multiplier (i.e. the @3x) that is used to convert the points into pixels. I was hoping that I could use:

let scaleFactor = view.contentScaleFactor // GIVES 1

But as you can see it always seems to return 1. I know that I can grab the screen size directly using
but I am sure there was a way before to access the multiplier too?

let yRes = viewBounds.height * scaleFactor // 736 x 3 = 2208
let xRes = viewBounds.width * scaleFactor // 414 x 3 = 1242

Answer Source

the UIView Class Reference documentation says about contentScaleFactor property:

(...) For system views, the value of this property may be 1.0 even on high resolution screens.

therefore that is not what you look for.

the actual scale factor can be read from the main screen, that will give you either 1.0, 2.0 or even 3.0:

var scale: CGFloat = UIScreen.mainScreen().scale // Swift


CGFloat _scale = [[UIScreen mainScreen] scale]; // Obj-C

you can read more about the scale property in the UIScreen Class Reference.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download