简体   繁体   中英

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

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?

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 .

You may want to consider working with the frame property of the view of the current view controller.

self.view.frame.size.height and self.view.frame.size.width

You can make your other views proportional to those dimensions or based on their values, etc.

This code should adjust your scroll view to the size of the users screen.

self.view.frame.size.height and self.view.frame.size.width

Also, make sure that your constraints are all good.

Use view.frame.size.height and view.frame.size.width to determine the size of different screen sizes. This will work inside functions only.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM