如何使UITableViewCell的ImageView具有固定大小,文本无法正确对齐?

时间:2022-07-11 21:45:09

How do I set imageView's size fixed in tableViewCell so that texts and images aligned properly..

如何在tableViewCell中设置imageView的大小,以便正确对齐文本和图像。

I have tried following things

我试过以下事情

     self.imageView?.frame.size.width = 40.0
     self.imageView?.frame = CGRectMake(0,0,40,40)

     cell.imageView.autoresizingMask = ( UIViewAutoresizingNone );
     self.imageView?.autoresizesSubviews = false
     cell.imageView.contentMode = UIViewContentModeCenter;
     self.imageView?.bounds = CGRectMake(0, 0, 50, 50);
     cell.imageView.frame = CGRectMake(0, 0, 50, 50);
     cell.imageView.image = [UIImage imageWithData: imageData];
     self.contentView.frame = self.bounds;

Thanks.

谢谢。

1 个解决方案

#1


0  

You can try to set width or height constraint from code.

您可以尝试从代码中设置宽度或高度约束。

yourImageView.translatesAutoresizingMaskIntoConstraints = false
let widthContsraint = NSLayoutConstraint(item: yourImageView, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 100 // your_value)
let heightConstraint = NSLayoutConstraint(item: yourImageView, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 100 // your_value)
yourImageView.addConstraints([widthContsraint, heightConstraint])

#1


0  

You can try to set width or height constraint from code.

您可以尝试从代码中设置宽度或高度约束。

yourImageView.translatesAutoresizingMaskIntoConstraints = false
let widthContsraint = NSLayoutConstraint(item: yourImageView, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 100 // your_value)
let heightConstraint = NSLayoutConstraint(item: yourImageView, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 100 // your_value)
yourImageView.addConstraints([widthContsraint, heightConstraint])