iphone: put uiimage in tableview section header, stretch problem

前端 未结 1 1679
既然无缘
既然无缘 2021-01-03 01:15

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         


        
相关标签:
1条回答
  • 2021-01-03 01:42

    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.

    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];
    }
    

    Hope this'll help!

    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.

    0 讨论(0)
提交回复
热议问题