2016-03-02 104 views
1

我试图加载图像到我的tableview单元格,但是,图像似乎比imageview更大,并超过了imageview的边界。 我试图将UIImageViewcontentMode属性设置为ScaleAspectFit,但它并未解决我的问题。图像不适合imageview在自定义tableview单元格

代码:

cell.imageView?.contentMode = UIViewContentMode.Center 
cell.imageView?.contentMode = UIViewContentMode.ScaleAspectFit 
switch (post.priorityLevel){ 
    case "High Priority Posting": 
     cell.imageView?.image = UIImage(named: "High Priority Posting") 
    case "Priority Posting": 
     cell.imageView?.image = UIImage(named: "Priority Posting") 
    case "General Posting": 
     cell.imageView?.image = UIImage(named: "General Posting") 
    default: 
     print("Priority Level can't be loaded to table") 
} 

我说从界面生成器和仿真结果我的表视图的屏幕截图。

任何建议,将不胜感激。 enter image description here

enter image description here

+0

可以在UI调试检查图像的大小,并检查所有约束。如果您可以向我们展示您为单元格设置了哪些约束条件,那么我们肯定可以帮助您。 – arjavlad

回答

2

按照以下约束,你会看到魔自动布局

ImageView的约束:

enter image description here

设置要为imageView显示的修复高度和宽度,并设置VerticalCentre约束。将imageView的内容模式设置为Aspect Fit。

顶标签约束:

enter image description here

标签TrailingSpace约束不应该是一个恒定值,而是设置为标准间距(即8分)。

第二标签约束:

enter image description here

标签TrailingSpace和BottomSpace约束不应该是一个恒定值,而是设置为标准间距(ⅰ。E 8分),并设置其VerticalContentHugging优先〜250

enter image description here

更新:

ImageView的垂直约束到细胞:

enter image description here

的Label1 TrailingSpace与标准间距细胞:

enter image description here

Label2的TrailingSpace与标准间距和BottomSpace到细胞与标准间距到小区。

enter image description here

+0

嗨巴拉特,我喜欢你的答案,但是你能告诉我在哪里添加verticalCentre约束和标签尾随空间?到处都找不到他们 – libra

+0

非常感谢你为你更新!!,这大概是我所#1 – libra

0

的UIImage

1)首先你设置前导空格,高度相等,中心垂直于容器 2)设置高度本身

的ImageView的做

城市和州

1)设定的水平空间和顶端到的UIImageView,尾随空间容器视图 2)设置高度本身

说明标签

1)设定的水平空间和底部到的UIImageView,尾随空间容器视图

2)设置高度本身 故事板 - >组线= 0

3

尝试cell.imageView .contentMode = UIViewContentMode.ScaleAspectFill

+0

很抱歉,这没有奏效T_T所收到的最好的答案,它看起来同 – libra

+1

所以可能是你的问题设计。 –

0

如果它的自定义表视图细胞,不命名的ImageView出口 “的ImageView”,它的名字到任何其他的如? “cellImageView”。然后尝试将clipsToBound设置为你的cell cellImageView的true。

相关问题