5

I'm working on an app that is ideally targeted all the way down to iOS 3.2. Still, I am developing it on Lion and with the latest 5 sdk. As far as I know, I am not using any sdk 5 specific features. But:

on any devices with iOS 5 or the simulator (set to v.5), the app works just fine. on any devices with iOS 4.3 or below (and the same goes for the simulator set to v. 4.3), several things that have to do with view frames get misaligned.

For instance, here's 2 examples:

An activity indicator inside an alert view. Here's the code:

NSURLConnection *urlConnection = [[NSURLConnection alloc] initWithRequest:fileRequest delegate:self];
  if(urlConnection) 
  {
    uistatusDialog = [[UIAlertView alloc] initWithTitle:(description ? NSLocalizedString(description, nil) : NSLocalizedString(@"Downloading", nil))
                                                message:nil
                                               delegate:nil 
                                      cancelButtonTitle:nil
                                      otherButtonTitles:nil];

    UIActivityIndicatorView *indicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
    indicator.autoresizingMask = (UIViewAutoresizingFlexibleLeftMargin  | 
                                  UIViewAutoresizingFlexibleRightMargin | 
                                  UIViewAutoresizingFlexibleTopMargin   | 
                                  UIViewAutoresizingFlexibleBottomMargin);

    [indicator startAnimating];
    [uistatusDialog addSubview: indicator];
    [uistatusDialog show];
    [indicator release];

And here are screenshots for both simulators:iOS 5: correct iOS 5: correct iOS 4.3: misaligned iOS 4.3: misaligned

Similar things are happening with labels for which I set frames through [UILabel alloc]initWithFrame:CGRectMake(...].

This code, for instance:

UITableViewCell * cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
                                                   reuseIdentifier:reuseIndentifier] autorelease];

  cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
  cell.selectionStyle = UITableViewCellSelectionStyleGray;

  UILabel* mainLabel = [[[UILabel alloc] initWithFrame:CGRectMake(70, 0, 0, 20)] autorelease];
  mainLabel.font = [UIFont boldSystemFontOfSize:(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad ? 18 : 12)];
  mainLabel.textAlignment = UITextAlignmentLeft;
  mainLabel.textColor = [UIColor blackColor];
  mainLabel.backgroundColor = [UIColor clearColor];
  mainLabel.autoresizingMask = (UIViewAutoresizingFlexibleHeight | 
                                UIViewAutoresizingFlexibleWidth  | 
                                UIViewAutoresizingFlexibleRightMargin);
  //mainLabel.adjustsFontSizeToFitWidth = YES;
  mainLabel.tag = MAINLABEL_TAG;

Aligns just fine in iOS5, both for the simulators and devices. But in 4.3 it doesn't. I can only think that the local coordinate frame changed from one SDK to the next?

Any help is greatly appreciated!

EDIT: Just to pull it off for now, I did end up replacing all instances of CGRectMake(x,y,w,h) with something along the lines of (assuming x,y,w,h are the ones I would have used for CGRectMake):

CGrect refFrame = superview.frame;
refFrame.origin.x += x;
refFrame.origin.y += y;
refFrame.size.w = w;
refFrame.size.h = h;
theObjInQuestion.frame = refFrame;

So essentially, looks like a different frame of reference is being used between SDK 5 and 4.3 at least...

Lebyrt
  • 1,376
  • 1
  • 9
  • 18
SaldaVonSchwartz
  • 3,769
  • 2
  • 41
  • 78
  • thing is this is not about the graphics (as in raster data). What's going on here is, as far as I can tell, the coordinate system being different. But that doesn't make sense since, technically, a views's frame is expressed in points (not pixels) and relative to the superview's local coordinate system (probably using homogeneous coordinates through transformation matrices.). So If I say CGRectMake(0,0,w,h), that means an area of wxh, with its upper left corner at the upper-left corner of the subview, regardless of the device resolution and (hopefully) regardless of the os version. – SaldaVonSchwartz Nov 15 '11 at 01:49
  • Perhaps you could try to do your positioning relative to the surrounding cell, e.g. don't give constants for x and y positions when creating the label but rather derive them from your cell like `(cell.frame.size.width - 20)` / 2 when you're creating a 20 point width label. This you you're not dependent on the sizes cells are created before actual layout. – Dennis Bliefernicht Nov 15 '11 at 02:13
  • I just added an EDIT with a workaround to my question. But, thing is, in response to your suggestion, that I am positioning in a relative way. When I say "70 to the right" I am saying 70 points to the right of the upper left origin of the superview. This is relative, and independent of resolution and positioning and size of the cell within UISCreen. The 70 in the above example, mind you, is because the cell has a custom imageView subclass that is 57 points wide (also relative). – SaldaVonSchwartz Nov 15 '11 at 03:10
  • Adding [view setAutoresizingMask:UIViewAutoresizingNone]; to EVERYTHING fixed this for me. – Oh Danny Boy May 22 '12 at 14:03

2 Answers2

2

I had a similar issue with one UIImageView in our app being displaced downwards about 100pts on screen, appearing to be displaced by other content that should have been floating on top of the UIImageView (though that may have been a coincidence).

The 'solution' I found in our case was to disable auto-sizing for the top positioning attribute for the UIImageView in IB, by clicking on the red I in the Autosizing display on the Size Inspector in Interface Builder. I call this a 'solution' rather than a solution because it remains unclear to me why this was a problem at all, and why this only occurred for this one view and only in iOS 5.

I also found that repositioning this view up, or down, prevented it from being displaced. It was only when it was aligned with the top edge of its parent view that the issue occurred.

My conclusion was it was probably a bug in iOS 5, rather than a new intended or more strict behavior, but I remain uncertain.

Duncan Babbage
  • 19,972
  • 4
  • 56
  • 93
0

There are some major differences between 4 and 5, though I've only begun to figure them out. Something has changed in the coordinate systems, but precisely what I don't know.

I kinda suspect that the best/safest thing to do is to have two entirely different paths for calculating layout, until someone can figure out all of the "gotchas". That way the two versions can be "tuned" separately.

Hot Licks
  • 47,103
  • 17
  • 93
  • 151