简体   繁体   English

在UIView中时不需要的UIImageView调整大小

[英]unwanted UIImageView resizing when in a UIView

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. 我正在以编程方式为宽度设置为290的表创建自定义节标题。当我使用包含290 x 29 PNG的UIImageView时,图像会正确显示。 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. 但是,我正在重构以包括UILabel,以便可以执行一些自定义文本,因此可以将UIImageView和UILabel放在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. 我尝试使用headerView.contentMode属性来进行任何更改,但到目前为止还算不上什么。

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

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. 我完全摆脱了UIView,最终将UILabel用作UIImageView的子视图,并返回了UIImageView。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM