UIImageView setFrame: not working in UITableViewCell

谁说胖子不能爱 提交于 2019-12-11 05:14:02

问题


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


来源:https://stackoverflow.com/questions/13890299/uiimageview-setframe-not-working-in-uitableviewcell

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!