How do I make UITableViewCell's ImageView a fixed size even when the image is smaller

后端 未结 16 2525
不思量自难忘°
不思量自难忘° 2020-11-27 10:27

I have a bunch of images I am using for cell\'s image views, they are all no bigger than 50x50. e.g. 40x50, 50x32, 20x37 .....

When I load the table view, the tex

16条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-11-27 10:43

    The regular UITableViewCell works well to position things but the cell.imageView doesn't seem to behave like you want it to. I found that it's simple enough to get the UITableViewCell to lay out properly by first giving the cell.imageView a properly sized image like

    // Putting in a blank image to make sure text always pushed to the side.
    UIGraphicsBeginImageContextWithOptions(CGSizeMake(kGroupImageDimension, kGroupImageDimension), NO, 0.0);
    UIImage *blank = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    cell.imageView.image = blank;
    

    Then you can just connect up your own properly working UIImageView with

    // The cell.imageView increases in size to accomodate the image given it.
    // We don't want this behaviour so we just attached a view on top of cell.imageView.
    // This gives us the positioning of the cell.imageView without the sizing
    // behaviour.
    UIImageView *anImageView = nil;
    NSArray *subviews = [cell.imageView subviews];
    if ([subviews count] == 0)
    {
        anImageView = [[UIImageView alloc] init];
        anImageView.translatesAutoresizingMaskIntoConstraints = NO;
        [cell.imageView addSubview:anImageView];
    
        NSLayoutConstraint *aConstraint = [NSLayoutConstraint constraintWithItem:anImageView attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:cell.imageView attribute:NSLayoutAttributeCenterX multiplier:1.0 constant:0.0];
        [cell.imageView addConstraint:aConstraint];
    
        aConstraint = [NSLayoutConstraint constraintWithItem:anImageView attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:cell.imageView attribute:NSLayoutAttributeCenterY multiplier:1.0 constant:0.0];
        [cell.imageView addConstraint:aConstraint];
    
        aConstraint = [NSLayoutConstraint constraintWithItem:anImageView attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:0.0 constant:kGroupImageDimension];
        [cell.imageView addConstraint:aConstraint];
    
        aConstraint = [NSLayoutConstraint constraintWithItem:anImageView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:0.0 constant:kGroupImageDimension];
        [cell.imageView addConstraint:aConstraint];
    }
    else
    {
        anImageView = [subviews firstObject];
    }
    

    Set the image on anImageView and it will do what you expect a UIImageView to do. Be the size you want it regardless of the image you give it. This should go in tableView:cellForRowAtIndexPath:

提交回复
热议问题