Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Swift, how to determine the physical size of a device screen?

Tags:

ios

swift

I am adding views programatically. For example, I have a scrollView as a top-bar menu, I set its height to 60. (I am already using auto layout for placing these views)

This top-bar menu looks alright visually in an iPad. However, in an iPhone, it's kind of occupying too much space. Therefore, I am thinking to change the height to 45 for an iPhone.

What's the way to get the physical size of the screen? (not screen resolution) So I can make the height proportional to the physical size?

like image 658
Joe Huang Avatar asked Jan 07 '23 23:01

Joe Huang


1 Answers

You can get this information accessing UIScreen singleton.

To get size use UIScreen.main.nativeBounds. Also you can get scale coefficient of screen UIScreen.main.nativeScale.

like image 192
rkyr Avatar answered Feb 23 '23 08:02

rkyr