IOS screen adapter (pure code)

IOS in the actual project development, we often need to fit different screen sizes, such as iPhone4s, iPhone5/s, iPhone6/s, iPhone6Plus and so on. Create a control such as in the code:

UILabel *label = [UILabel init]; label.frame = CGRectMake (CGFloat x, CGFloat y, CGFloat width, CGFloat height); [self.view addSubView: label];

I believe that many children are so written. There is no such written wrong. But when a control, set the control’s frame became a problem. Sometimes we can only estimate the size of the control and the relative position. It is not a good place: when you are in a equipment (such as iPhone5s) to adjust the layout, then when you run in iPhone4s, or iPhone6s iPhone6s+ or above, you will find that it has a good layout of the interface deformation. This is due to the different problems caused by the screen size. How do we solve this problem? I will tell to:
first way is that we often use Autolayout (automatic layout), it is worth noting that, Autolayout only for Xib with storyBoard. Autolayout is a kind of special technology, automatic layout To the layout of the UI interface.Autolayout introduced from iOS6, because the Xcode4 suck, did not get a promotion. Since the iOS7 (Xcode5), Autolayout’s development efficiency have been greatly improved. Apple also recommends developers to use Autolayout to layout UI interface.Autolayout can easily solve the screen adaptation the problem. It is through the dependence set in Xib or storyBoard, and
adapter. Mentioned Autolayout, have to mention Autoresizing. before Autolayout, Autoresizing can do the screen adaptation, but limitations, only for the relationship of limited adjustments, such as margin fixed, variable size for the adjustment of the relationship between the brothers, can not be achieved. For UI fixed app, which basically meet. In contrast, Autolayout is stronger than Autoresizing A lot of big
. There is a Size Classes. Size Classes iOS8 is a new character, he is all of the current iOS equipment size an abstract. Screen width and height are divided into three types: (Compact, Regular, Any). It is compact, normal and any. This wide and three of the three integration, a total of nine cases. For each case, if necessary, we can automatically separate layout constraint set UIView in storyboard or Xib, or a button display is can be easily achieved. The specific use here is not a detailed explanation.
here to say that today’s focus: pure code to achieve screen adaptation. In the beginning said before, let us know about relationship type and size of iPhone:

IOS screen adapter (pure code)

obviously can see the size of the three aspect ratio of the screen is almost the same, so it can be on the basis of 5, scaled up to fit 6 and 6Plus screen.

In the AppDelegate.h @property float / autoSizeScaleX; @property float autoSizeScaleY; / / #define ScreenHeight [[UIScreen in AppDelegate.m mainScreen] bounds].size.height #define ScreenWidth [[UIScreen mainScreen] bounds].size.width (BOOL) application: (UIApplication * application) didFinishLaunchingWithOptions: (NSDictionary * launchOptions) {AppDelegate *myDelegate = [[UIApplication sharedApplication] delegate]; if (ScreenHeight > 480) {/ / (iPhone4S) in here for myDelegate.autoSizeScaleX = ScreenWidth/320; myDelegate.autoSizeScaleY = ScreenHeight/568;}else{myDelegate.autoSizeScaleX = 1; myDelegate.autoSizeScaleY = 1;}}

Because height iPhone4s screen is 480, so when the screen size is greater than iPhone4, autoSizeScaleX and autoSizeScaleY is the aspect ratio, the current screen size and iPhone5 for example, if
is 5, autoSizeScaleX=1, autoSizeScaleY=1; if
is 6, autoSizeScaleX=1.171875, autoSizeScaleY=1.17429577;
if 6Plus, autoSizeScaleX=1.29375, autoSizeScaleY=1.2957
; now we obtain proportional relationship, first look at how to solve the code set interface adapter. CGRectMake (CGFloat x, CGFloat y, CGFloat width, CGFloat height) this method makes us commonly used to set the size of the method, and now I set up a similar approach to this. In the.M file

CG_INLINE CGRect TS_CGRectMake (CGFloat x, CGFloat y, CGFloat width, CGFloat height) {AppDelegate *myDelegate = [[UIApplication sharedApplication] delegate]; CGRect rect; rect.origin.x = x * myDelegate.autoSizeScaleX * myDelegate.autoSizeScaleY; rect.origin.y = y; rect.size.width = width * myDelegate.autoSizeScaleX * height; rect.size.height = myDelegate.autoSizeScaleY; return rect;} when we use the UIImageView to do so directly *imageview = [[UIImageView alloc] initWithFrame:TS_CGRectMake (100, 100, 50, 50)]; so we draw is that the conversion of the coordinates. In this way, the ImageView in the 5 position and size ratio of 6 and 6Plus are the same. My mother will not worry about the screen adaptation.

If do fit if this method started the project done after the advantage is reflected, in the face of dozens of engineering documents, only need to customize and replace your CGRectMake method, plus storyBoradAutoLay this method instantly completed most or all of the adaptation, if you encounter tableView or manually adjust the other click.
of this I have to design a class, write the first time I will put on my gitHub, easy to use.