title: Programmatically adapt uiviews to iPad / iPhone screens
body: <p>I make an app that has many views that subclass from UIView. The size and the orientation of these views is random and the state of a screen of the app can be saved. When the user saves a screen on the same device that he opens it, then the screen state is OK. Everything is positioned correctly. But, if the user saves the screen state on an iPhone and opens it from an iPad the views are not positioned correctly. Actually the views appear shorter or longer, the center seems to be saved correctly, but the rotation of the views and their size (bounds property) are not working OK.</p>
  
  <p>These are the two methods that save and restore the state of the view</p>
  
  <pre><code>- (void)encodeWithCoder:(NSCoder *)aCoder {
      // Save the screen size of the device that the view was saved on
      [aCoder encodeCGSize:self.gameView.bounds.size forKey:@"saveDeviceGameViewSize"];
  
      // ****************
      // ALL properties are saved in normalized coords
      // ****************
  
      // Save the center of the view
      CGPoint normCenter = CGPointMake(self.center.x / self.gameView.bounds.size.width, self.center.y / self.gameView.bounds.size.height);
      [aCoder encodeCGPoint:normCenter forKey:@"center"];
  
       // I rely on view bounds NOT frame
      CGRect normBounds = CGRectMake(0, 0, self.bounds.size.width / self.gameView.bounds.size.width, self.bounds.size.height / self.gameView.bounds.size.height);
      [aCoder encodeCGRect:normBounds forKey:@"bounds"];
  
       // Here I save the transformation of the view, it has ONLY rotation info, not translation or scalings
      [aCoder encodeCGAffineTransform:self.transform forKey:@"transform"];
  }
  
  - (id)initWithCoder:(NSCoder *)aDecoder {
      self = [super initWithCoder:aDecoder];
      if (self) {
  
          // Restore the screen size of the device that the view was saved on
          saveDeviceGameViewSize = [aDecoder decodeCGSizeForKey:@"saveDeviceGameViewSize"];
  
          // Adjust the view center
          CGPoint tmpCenter = [aDecoder decodeCGPointForKey:@"center"];
          tmpCenter.x *= self.gameView.bounds.size.width;
          tmpCenter.y *= self.gameView.bounds.size.height;
          self.center = tmpCenter;
  
          // Restore the transform
          self.transform = [aDecoder decodeCGAffineTransformForKey:@"transform"];
  
          // Restore the bounds
          CGRect tmpBounds = [aDecoder decodeCGRectForKey:@"bounds"];
          CGFloat ratio = self.gameView.bounds.size.height / saveDeviceGameViewSize.height;
          tmpBounds.size.width *= (saveDeviceGameViewSize.width * ratio);
          tmpBounds.size.height *= self.gameView.bounds.size.height;
          self.bounds = tmpBounds;
      }
      return self;
  }
  </code></pre>