UIImageView setFrame: not working in UITableViewCe

2019-07-27 06:04发布

问题:

I've an UIImageView in customized UITableViewCell and I want to resize it, I set it view mode to LEFT. To resize I'm applying below code.

[cell.IBImage setFrame:CGRectMake(201,12,50,20)];

My image width is 100 and I want to reduce is to 50 but it still shows me in 100 width. If I set view mode to Scale To Fill it works fine but I want to have it's view mode to LEFT.

Full method cellForRowAtIndexPath is as given below:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

static NSString *CellIdentifier = @"DetailCell";

DetailCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"DetailCell" owner:self options:nil];

cell = (DetailCell *)[topLevelObjects objectAtIndex:0];

clsDetailResponse *Response = [self.Histories objectAtIndex:[indexPath row]];

[cell.IBUsernameLabel setText:[NSString stringWithFormat:@"%@ %@", Response.Firstname, Response.Lastname]];

[cell.IBImage setFrame:CGRectMake(201, 12, 50, 20)];

return cell;
}

回答1:

You are doing to resize the imageView frame not the image so You can crop your image as you want,

UIImage *ima = cell.image.image;
CGRect cropRect = CGRectMake(0, 0, 50,20);
CGImageRef imageRef = CGImageCreateWithImageInRect([ima CGImage], cropRect);
[cell.image setImage:[UIImage imageWithCGImage:imageRef]];
CGImageRelease(imageRef);
cell.image.frame=CGRectMake(10, 10, 50, 20);


回答2:

set IBImage frame in DetailCell class in

-(void)layoutSubviews:

method



回答3:

Add the following lines to the File's owner to make sure it knows how to respond to framechanges.

    [self setAutoresizingMask:UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth];
    [self setClipsToBounds:YES];
    [self setAutoresizesSubviews:YES];