iphone:将uiimage放在tableview部分标题中,拉伸问题 [英] iphone: put uiimage in tableview section header, stretch problem

查看:23
本文介绍了iphone:将uiimage放在tableview部分标题中,拉伸问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,我将 UIImage 放入 UITableVIew 的部分标题中.因此我使用了我在互联网上找到的一些代码:

hey people, I put an UIImage into the section header of my UITableVIew. Therefor I used some code I found on the internet:

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{

self.imageViewForImage.image = [helperClass resizeImage:[self.offerItem objectForKey: @"picture"] forSize:CGSizeMake(280.0, 280.0) ];

[self.imageViewForImage.layer setBorderColor: [[UIColor blackColor] CGColor]];
[self.imageViewForImage.layer setBorderWidth: 1.2];

self.imageViewForImage.contentMode = UIViewContentModeBottom;
return self.imageViewForImage;}


-(CGFloat)tableView:(UITableView*)tableView heightForHeaderInSection:(NSInteger)section
{
    if(section == 0)
        return 320;
    return 1.0;
}

起初,图像完全按 iphone 的 320 像素宽度进行缩放.但后来我发现,我的整个 UITableView 的 contentMode-property 设置为UIViewContentModeScaleToFill".所以我添加了一行代码,告诉我的 UIImageView 不要进行缩放.就我而言,我将其设置为UIViewContentModeBottom".

At first the image was totally scaled over the complete 320px width of the iphone. But then I found out, that my whole UITableView's contentMode-property was set to "UIViewContentModeScaleToFill". So I added a line of code, which tells my UIImageView not to do the scaling. In my case I set it to "UIViewContentModeBottom".

问题:这一切都有效,图像最终以 280x280 的纵横比显示,但我在图片周围做的边框仍然被拉伸/调整为 iphone 的完整宽度......?!

Problem: This whole thing works, the image is finally shown in the aspected size of 280x280, BUT the border I did around the picture is still stretched / resized to the complete width of the iphone...?!

我不知道如何解决这个问题,因为我想要 UIImage 周围的边框..

I can't figure out how to resolve this issue, because I want the border around the UIImage..

感谢您对此的任何帮助..

Thanks for any help on this..

有人知道吗,这里出了什么问题?

edit: does anyone has some idea, what is going wrong here?

推荐答案

您的 UIImageView 有一个图层,您将其内容设置为图像.层的大小等于imageView的大小,但图像的大小小于它.因此,当您将 contentMode 设置为您的视图时,如果内容大小小于图层大小,它会告诉它的图层在底部绘制图层内容.因此,即使您调整了其内容图像的大小,图层的大小(以及其框架的大小)仍然相同.

Your UIImageView has a layer the contents of which you set to your image. The size of layer equals to the size of imageView, but size of image is less then it. So when you set contentMode to your view it says to it's layer to draw layers contents at bottom if contents size is less than layers size. So size of layer (and as consequence size of its frame) are still the same even though you have resized its contents image.

问题是您正在尝试调整图像大小,而您应该调整 UIImageView 的大小.这部分代码应该如下所示:

The problem is that you are trying to resize image, when you should resize UIImageView. This part of code should look something like this:

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *headerView = [[UIView alloc] init];

    self.imageViewForImage.frame = CGRectMake(20, 20, 280, 280);
    self.imageViewForImage.image = [self.offerItem objectForKey:@"picture"];
    self.imageViewForImage.autoresizingMask = UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin;

    [self.imageViewForImage.layer setBorderColor: [[UIColor blackColor] CGColor]];
    [self.imageViewForImage.layer setBorderWidth: 1.2];

    [headerView addSubview:self.imageViewForImage];
    return [headerView autorelease];
}

希望这会有所帮助!

更新:问题是该方法返回的视图被 tableView 重构以填充所有标题.所以我们必须创建一个实际上是标题视图的视图,并在其上放置一个 imageView.

UPDATED: The issue was that view that this method returns is reframed by tableView to fill all the header. So we must make a view that will actually be a header view and place an imageView on it.

这篇关于iphone:将uiimage放在tableview部分标题中,拉伸问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆