views:

365

answers:

1

I'm programatically creating a custom section header for a table that has a width set to 290. When I use a UIImageView containing a 290 x 29 PNG directly the image appears correctly. However, I'm refactoring to include a UILabel so I can do some custom text so I place the UIImageView and UILabel in a UIView. When I view this version, the image is about three pixels narrower than it should be.

In other words:

UIImageView * headerImg = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"subtable-header.png"]];
return headerImg; // this works correctly.  The image is "full size" at 290px wide.

However when I try this:

UIView * headerView = [[[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 290.0f, 29.0f)] autorelease];
UIImageView * headerImg = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"subtable-header.png"]];
[headerView addSubview:headerImg];
[headerImg release];
return headerView;  // The image is about 3 pixels narrower

I've tried working with the headerView.contentMode property to see that has any changes, but no luck so far.

One more note:

NSLog(@"view frame size: %1.2f %1.2f %1.2f %1.2f", headerView.frame.size.width, headerView.frame.size.height, headerView.frame.origin.x, headerView.frame.origin.y);
NSLog(@"image frame size: %1.2f %1.2f %1.2f %1.2f", headerImg.frame.size.width, headerImg.frame.size.height, headerImg.frame.origin.x, headerImg.frame.origin.y);

Returns:

2009-11-20 11:35:42.323 MyApp[1350:20b] view frame size: 290.00 29.00 0.00 0.00
2009-11-20 11:35:42.323 MyApp[1350:20b] image frame size: 290.00 29.00 0.00 0.00
A: 

Not really a solution, but this is what ended up working. I got rid of the UIView completely and ended up putting the UILabel as a subview of the UIImageView and returned the UIImageView.

GSP