How to convert frame based on device screen size?

I am working on a dynamic application in which I get the CGRect values ​​individually (x, y, width, height) from the web service for each of the user interface objects inside the view.

On the server, they will set frames based on the following size (1080 x 1920), so let's say if I get a CGRect with the following values: (200, 20, 100, 100), then this should fit into the device based on the size of the device and the specified server size.

  • So, with the iPhone 5 - the new frame will be the same 200, 20, 100, 100.

  • So, in the case of the iPhone 6 (375 x 667) - will there be a new frame?

  • So, in the case of the iPhone 6+ (414 x 736) - will there be a new frame?

The main thing is that no matter what frame will be accepted in the application for a specific user interface object, it should look the same as in the admin panel specifically for fields.

For a note

I configure the interface with AutoLayout only in the storyboard.

Any suggestion?

This is an example:

Example 1:

 | _________________ | | | | | | | | | | | | | | | | | | |_________________| | | | 

Example 2:

 | _________ | | | | | | | | | | | | | | | | | | |_________| | | | 

In the examples above, I'm trying to explain that any frame will come from the server if it looks like a server similar to a device, but it should be based on a CGRect device.

+5
source share
3 answers

You can use these two methods to get the server and CGRect device .

 - (CGFloat) convertServerXorWidthValueToDeviceValueXorWidth:(CGFloat)sValue { CGFloat currentWidth = [UIScreen mainScreen].bounds.size.width; CGFloat value = ceilf((currentWidth * sValue) / 1080); return value; } - (CGFloat) convertServerYorHeightValueToDeviceValueYorHeight:(CGFloat)sValue { CGFloat currentHeight = [UIScreen mainScreen].bounds.size.height; CGFloat value = ceilf((currentHeight * sValue) / 1920); return value; } 

Hope this helps you.

+1
source

The base for changing frames on the screen size and the ratio of iPhone 4s, 5.6.6 +.

Here you need to change the frame size on the screen, so that you have "frames based on the next size (1080 x 1920)" , which means that you have an iPhone 6+, now you see that the following set of images is based on .

1). IPhone 5 frame (200,20,100,100)
2). IPhone 6 frame (200,20,155,155)
3). iPhone 6+ (200,20,194,194)

enter image description here
Walkie-talkie screen with screen resolution. I get From This Site. Screen aspect ratio

imgur .com / 7zi1q.png

+2
source

I would say that the frame is for iPhone 6 (234, 23, 117, 117) and iPhone 6 Plus (258, 25, 129, 129).

How did I calculate? New width = (old width * new device width) / old device width

(100 * 375) / 320 = 117

same for other values

When using automatic layouts, you need to update the constraints after receiving new values.

0
source

Source: https://habr.com/ru/post/1244884/


All Articles