簡體   English   中英

在TableViewCell內設置UIImage時,約束不起作用

[英]Constraints don't work when setting an UIImage inside a TableViewCell

當我在cellForRowAt函數中設置UIImage ,它看起來像我正在設置的UIImageUIImageView重疊。

編輯: clipsToBound標記和contentMode設置為.scaleAspectFitStoryboard

我照常在Storyboard設置所有Constraints

在此處輸入圖片說明

但是,當我運行該應用程序時,它看起來比設置的Constraints具有更高的優先級,這就是我得到的:

在此處輸入圖片說明

不久前,我在其他項目中做了類似的事情,對我來說效果很好:

在此處輸入圖片說明

這是用於設置單元格的代碼,盡管我敢肯定我只是在Storyboard意外標記或未標記了一些東西:

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    return 100
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return songArray.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    guard let cell = tableView.dequeueReusableCell(withIdentifier: MainVCCell.CELL_ID, for: indexPath) as? MainVCCell else { return UITableViewCell() }
    cell.showLoader()
    cell.songNameLabel.text = songArray[indexPath.row].title

    if songImageArray[songArray[indexPath.row].title] != nil {
        cell.imageView?.image = songImageArray[songArray[indexPath.row].title]!!
        cell.hideLoader()
    }
    cell.setupImageDesign()
    return cell
}

如果需要任何其他信息,請讓我知道,謝謝。

將圖像視圖clipsToBounds設置為true

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    guard let cell = tableView.dequeueReusableCell(withIdentifier: MainVCCell.CELL_ID, for: indexPath) as? MainVCCell else { return UITableViewCell() }
    cell.showLoader()
    cell.songNameLabel.text = songArray[indexPath.row].title

    if songImageArray[songArray[indexPath.row].title] != nil {
        cell.imageView?.image = songImageArray[songArray[indexPath.row].title]!!
        cell.hideLoader()
    }
    cell.setupImageDesign()
    return cell
}

cell.imageView?.image是UITableViewCell中的默認cell.imageView?.image
您必須重命名自定義UIImageView出口

在情節clipsToBounds = true更改UIImageView的contentMode ,還設置clipsToBounds = true

將ImageView的ContentMode更改為.scalToFill並將Clipping屬性更改為clipToBounds

對邊距約束存在疑問

請刪除所有約束,並確保在設置約束之前取消選中此選項。

在此處輸入圖片說明

在情節提要中看起來像這樣

在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM