繁体   English   中英

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

[英]unwanted UIImageView resizing when in a UIView

我正在以编程方式为宽度设置为290的表创建自定义节标题。当我使用包含290 x 29 PNG的UIImageView时,图像会正确显示。 但是,我正在重构以包括UILabel,以便可以执行一些自定义文本,因此可以将UIImageView和UILabel放在UIView中。 当我查看此版本时,图像大约比应该窄三个像素。

换一种说法:

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

但是,当我尝试这样做:

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 

我尝试使用headerView.contentMode属性来进行任何更改,但到目前为止还算不上什么。

还有一点注意事项:

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);

返回值:

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

并不是真正的解决方案,但这才最终奏效。 我完全摆脱了UIView,最终将UILabel用作UIImageView的子视图,并返回了UIImageView。

暂无
暂无

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

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