在 UIView 中时不需要的 UIImageView 调整大小
我正在以编程方式为宽度设置为 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
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这并不是一个真正的解决方案,但这就是最终有效的方法。我完全摆脱了 UIView 并最终将 UILabel 作为 UIImageView 的子视图并返回了 UIImageView。
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.