Pure code adaptation
Look at the screen aspect ratio of each model Apple
4.0 inches ( the iPhone. 5, the iPhone 5S ) aspect ratio of 320/568 = 0.563
4.7 inch ( the iPhone. 6, the iPhone 6S, the iPhone. 7, the iPhone 7S ) aspect ratio of 375/668 = 0.562
5.5 inch ( the iPhone Plus. 6, the iPhone 6S Plus, the iPhone. 7 Plus, the iPhone 7S Plus ) aspect ratio of 414/736 = 0.5625
It can be seen that substantially the same aspect ratio, the resolution can be calculated in the frame on which the screen according to a further screen, as follows:
// get the screen width and height
#define ScreenWidth [UIScreen mainScreen].bounds.size.width;
#define ScreenHeight [UIScreen mainScreen].bounds.size.height;
// to 4.0 -inch screen ( iPhone5s ) as a reference, it can also be 4.7-inch or 5.5-inch screen as the base
#define AutoSizeScaleX [UIScreen mainScreen].bounds.size.width / 320.0;
#define AutoSizeScaleY [UIScreen mainScreen].bounds.size.height / 568.0;
// Inline method defined macro size
CG_INLINE CGRect MSRectMake(CGFloat x, CGFloat y, CGFloat width, CGFloat height)
{
CGRect rectum;
rect.origin.x = x * AutoSizeScaleX;
rect.origin.y = y * AutoSizeScaleY;
rect.size.width = width * AutoSizeScaleX;
rect.size.height = height * AutoSizeScaleY;
return rect;
}
用法:用MSRectMake替换换系统的RectMake即可
UIView *myView = [[UIView alloc] initWithFrame:MSRectMake(15, 64 + 15, 290, 50)];
myView.backgroundColor = [UIColor blackColor];
[self.view addSubview:myView];