Practical tips for iOS screen adaptation development

  • 2020-12-10 00:53:00
  • OfStack

1. Spin treatment

Step 1: Register the notification


[[NSNotificationCenter defaultCenter] addObserver:self
    selector:@selector(changeFrames:)                           
  name:UIDeviceOrientationDidChangeNotification
     object:nil];  

Step 2: Process the receive event


-(void)changeFrames:(NSNotification *)notification{
  NSLog(@"change notification: %@", notification.userInfo);
  float width=[[UIScreen mainScreen]bounds].size.width*[[UIScreen mainScreen] scale];
  float height=[[UIScreen mainScreen]bounds].size.height*[[UIScreen mainScreen] scale];
  if ([[UIDevice currentDevice] orientation]==UIInterfaceOrientationPortrait
    || [[UIDevice currentDevice] orientation]==UIInterfaceOrientationPortraitUpsideDown) {
    NSLog(@">>>portrait");
    self.frame=CGRectMake(0, 0, height, width);
  }else{
    NSLog(@">>>landscape");
    self.frame=CGRectMake(0, 0, width, height);
  }
  
  NSLog(@"view - > %@",self);
}

2. Get the screen resolution


 // Get the current screen size: 
  CGSize size_screen = [[UIScreenmainScreen]bounds].size;
  // Obtain the zoom ratio: 
  CGFloat scale_screen = [UIScreen mainScreen].scale;  

   The width and height of the screen size scale The product of is the corresponding resolution value. 
  CGRect sizeOfA4 = CGRectMake(0, 0, 595, 842);// generate PDF File according to the A4 standard 
  CGRect sizeOfA5 = CGRectMake(0, 0, 421, 595);// generate PDF File according to the A5 standard 

Note: whether scale=1 or scale=2, the paper standard sizeOfA4 and sizeOfA5 are set the same, this is because we usually set the width and height under the iOS system are logical point, not real pixels!

As long as the screen is scaled up or down, [[UIScreenmainScreen]bounds].size remains the same. Different scale systems zoom in and out automatically, which is why all applications operate in 320x480 and 640x960 environments without any difference.

3. Equipment standards

iPhone/iPod Touch (320 x 480)
x 480 pixels, 320 pixels
Retina resolution 640 pixels x 960 pixels

iPad,iPad2/New iPad (768 points x 1024)
x 1024 pixels, 768 pixels
Retina screen 1536 pixels x 2048 pixels

Conversion relation (1 Point on iPhone3 is equivalent to 1 pixel; One point on iPhone4 is equivalent to four pixel;)
1 point = 1 pixel image. png
Retina screen 1 point = 2 pixels image@2x.png

4. Real machine and simulator judgment + device type judgment


 #if defined(TARGET_IPHONE_SIMULATOR) && TARGET_IPHONE_SIMULATOR
    NSLog(@" on simulator");
 #else
    NSLog(@"not on simulator");
#endif

Note: TARGET_OS_IPHONE is 1 on both the real machine and the simulator
There are two methods for judging device type:

1. UI_USER_INTERFACE_IDIOM() makes the distinction (ios 3.2 above), but cannot distinguish between iphone and ipod


    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
      // Equipment for ipad
    } else {
      // Equipment for iphone  or  ipod
    }

2. Use UIDevice. model for differentiation (ios 2.0 > =)


 NSString *deviceType = [UIDevice currentDevice].model;  
    if([deviceType isEqualToString:@"iPhone"]) {
       //iPhone
    }else if([deviceType isEqualToString:@"iPod touch"]) {
      //iPod Touch
    }else {
      //iPad
    }

5. Obtain device related information


  // Software information 
  NSLog(@"sysname=%@",[[UIDevice currentDevice] systemName]);//  System name 
  NSLog(@"systemVersion=%@",[[UIDevice currentDevice] systemVersion]); // The version number 
  NSLog(@"model=%@",[[UIDevice currentDevice] model]); // Type ( ipad , ipod , iphone ) and [[UIDevice currentDevice] userInterfaceIdiom] Can only judge iphone and ipad
  NSLog(@"olduuid=%@",[[UIDevice currentDevice] uniqueIdentifier]); // only 1 Identification code  ios5.0 start deprecated
  NSLog(@"name=%@",[[UIDevice currentDevice] name]); // Device name 
  NSLog(@"localizedModel=%@",[[UIDevice currentDevice] localizedModel]); //  Local mode 
  NSLog(@"ios6UUID=%@",[[[UIDevice currentDevice] identifierForVendor] UUIDString]);//ios6.0 start available
  
  ---------- Note: The following contents were not tested ---------------
  //  Hardware information 
  [UIDevice platform];// platform 
  [UIDevice cpuFrequency]];//cpu information 
  UIDevice busFrequency]];// The bus 
  [UIDevice totalMemory]];// The total memory 
  UIDevice userMemory]];// Memory already in use 
  -----------------------------------------------------------------------------------------------------------------------------
  //App information 
  NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary];
  CFShow(infoDictionary);// all plist content 
  // app The name of the 
  NSString *app_Name = [infoDictionary objectForKey:@"CFBundleDisplayName"];
  // app version 
  NSString *app_Version = [infoDictionary objectForKey:@"CFBundleShortVersionString"];
  // app build version 
  NSString *app_build = [infoDictionary objectForKey:@"CFBundleVersion"];
 
  // Determine if there is a camera 
  if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) 
    NSLog(@" There are ");
  else
    NSLog(@" There is no ");

6. For devices with different resolutions, programmers only need to do three things:

1. Provide app hd icon;
2.UI photo material @2x.png;
3. Download the adapted image from the Internet (judge conditions [[UIScreen mainScreen] respondsToSelector: @selector (scale)] & & [[UIScreen] mainScreen] == 2)
- The RESOLUTION of Point of all iPhone and iPod Touch devices is 320×480, that is, the logical resolution is 1, which ensures that App can run normally on high pixel resolution without modification. However, the images in the original App will be displayed after being pulled up, which will affect aesthetics, and the advantages of retina are not given full play.
- All GCRectMake within the program is the logical resolution, does not affect the location and size! Doing game development is the most useful. Regular app has little impact
- Question: How to do relative layout?? (autoResizeMask precedes 6.0, and autoresizing 6.0 can use autoLayout with a similar layout to android)


Related articles: