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

时间:2015-09-14 19:12:34

标签: ios iphone swift swift2

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

我尝试过以下事情

     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;

感谢。

1 个答案:

答案 0 :(得分:0)

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

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