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

发布于 2024-08-12 17:14:11 字数 1424 浏览 9 评论 0原文

我正在以编程方式为宽度设置为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

王权女流氓 2024-08-19 17:14:11

这并不是一个真正的解决方案,但这就是最终有效的方法。我完全摆脱了 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文