[英]Image not fit to imageview in custom tableview cell
我正在嘗試將圖像加載到我的tableview單元格中,但是,該圖像似乎比imageview大,並且超出了imageview的邊界。 我試圖將UIImageView
的contentMode
屬性設置為ScaleAspectFit
但沒有解決我的問題。
代碼:
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")
}
我在“界面”構建器中添加了表格視圖的屏幕截圖,並在模擬器上添加了結果。
遵循以下約束,您將看到自動布局的魔力
ImageView約束:
設置要為imageView顯示的固定高度和寬度,並設置VerticalCentre約束。 將imageView的內容模式設置為Aspect Fit。
最高標簽限制:
標簽TrailingSpace約束不應為常數,而應將其設置為標准間距(即8磅)。
第二標簽約束:
標簽TrailingSpace和BottomSpace約束不應為恆定值,而應將其設置為標准間距(即8 pts),並將其VerticalContentHugging優先級設置為250
更新:
ImageView對單元格的垂直約束:
Label1 TrailingSpace到具有標准間距的單元格:
Label2將TrailingSpace移至具有標准間距的單元格,並將BottomSpace移至具有標准間距的單元格。
嘗試cell.imageView?.contentMode = UIViewContentMode.ScaleAspectFill
在此官方教程中,他們設計與您完全一樣的表格視圖。 我認為您的主要問題與缺乏約束/自動布局有關
UIImage
1)首先設置imageview的前導空間,等高,垂直於容器的中心2)設置高度本身
城市和州
1)將水平空間和頂部設置為UiimageView,將空間拖到容器視圖2)設置高度本身
說明標簽
1)將水平空間和底部設置為UiimageView,將空間拖到容器視圖
2)設置故事板本身的高度->設置線= 0
如果是自定義表格視圖單元,則不要將imageView出口命名為“ imageView”,而將其命名為其他名稱,例如:“ cellImageView”。 然后嘗試將單元格cellImageView的clipsToBound設置為true。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.