I want to create a UIView that takes up a one inch square on every iOS device. So far, I have looked up the pixel size of my current test-device and translated a CGFloat to an inch using my test-device's pixel size times its UIScreen's scale factor. Is there any clever way to use built-in information about the UIDevice to get a CGFloat that amounts to one inch across the screen when applied to a UIView's width and height? Instead of creating cases where I manually account for every possible device pixel size, then have to update it every time a new pixel size comes out, I would like to somehow figure out the pixel size using the device's characteristics.
Asked
Active
Viewed 108 times
1
-
Does this answer your question? [How to find PPI programmatically with precision](https://stackoverflow.com/questions/36618965/how-to-find-ppi-programmatically-with-precision) – NobodyNada May 25 '20 at 18:27
-
It has some useful information but does not exactly get to what I am looking for. Maybe the referenced CocoaPod in that answer is up-to-date and would do what I want to do, but I am hoping that someone could come out with a straightforward formula using some of the built-in device information that gets from CGFloats in a CGSize to inches or cm or any other physical distance unit. – KhanKhuu May 25 '20 at 18:39
-
The second answer to that question says that "there is no public API to get either PPI or physical size of a screen. The only way is to hardcode list of devices with their physical sizes and/or PPI's." – NobodyNada May 25 '20 at 18:41
-
I went ahead and used the CocoaPod in the answer to the question and it worked for what I am trying to do, and indeed it does use manually compiled device resolution/PPI values. I really want to believe there is a way to do this without compiling that data, but for now accept that most people believe there is not a way to do that. I may look into it more later to convince myself one way or the other. Thanks @NobodyNada-ReinstateMonica – KhanKhuu May 25 '20 at 20:04
1 Answers
-1
The extension at https://github.com/marchv/UIScreenExtension works to do what I want to do by compiling data about all possible screen PPIs and comparing the UIDevice Model against them to get the CGFloat representing a centimeter of physical distance. Thanks to NobodyNada - Reinstate Monica's comment leading me to this extension by linking How to find PPI programmatically with precision.
I am accepting for now that there may not be a way to get physical distance just from the UIDevice's intrinsic properties, as another answerer to that question suggests.

KhanKhuu
- 177
- 11