簡體   English   中英

使用imageView調整圖標大小

[英]Resizing icon using imageView

我有以下代碼(已更新):

    // Set cell row height
override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat
{

    return UITableViewAutomaticDimension  // Auto-size cell based on content

}

// Set cell content
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)

    // Configure the cell...
    let weatherObject = forecastData[indexPath.section]

    // Convert UNIX time into readable format
    let sunriseUNIX = weatherObject.sunriseTime
    let sunriseDate = Date(timeIntervalSince1970: Double(sunriseUNIX))

    let sunsetUNIX = weatherObject.sunsetTime
    let sunsetDate = Date(timeIntervalSince1970: Double(sunsetUNIX))

    let dateFormatter = DateFormatter()
    dateFormatter.dateFormat = "h:mm a"
    let sunriseConv = dateFormatter.string(from: sunriseDate)
    let sunsetConv = dateFormatter.string(from: sunsetDate)


    cell.textLabel?.text = weatherObject.summary
    cell.detailTextLabel?.text = "Max: \(Int(round(weatherObject.temperatureMax))) °F / Min: \(Int(round(weatherObject.temperatureMin))) °F \nWill feel like \(Int(round(weatherObject.apparentTemperatureMax))) °F \nSunrise: \(sunriseConv)   Sunset: \(sunsetConv) \nRelative Humidity: \(Int((weatherObject.humidity)*100))% \nMoon Phase: \(round(weatherObject.moonPhase))"
    cell.detailTextLabel!.numberOfLines = 0
    cell.imageView?.image = UIImage(named: weatherObject.icon)
    cell.imageView?.frame = CGRect(x: 0, y: 0, width: 10, height: 10)
    cell.imageView?.contentMode = .scaleAspectFit
    cell.imageView?.clipsToBounds = true  // clipping will help check actual frame of image
    cell.layoutIfNeeded()
    cell.setNeedsDisplay()  // try to reset display, if not updating your image scale automatically

    return cell
}

...(仍然)產生以下結果:

代碼結果

我正在嘗試獲取大小為10 x 10像素的“圖標”圖像,但是上面的代碼不起作用。 無論CGRect中的值CGRect ,“圖標”的大小均保持不變。

任何指針,替代品將不勝感激!

提前致謝。

您應正確設置圖像的內容模式,以將其縮放到正確的尺寸。

cell.imageView.contentMode = .scaleAspectFit

您可以使用如下所示的代碼:

var newImgThumb : UIImageView
newImgThumb = UIImageView(frame:CGRectMake(0, 0, 100, 70))
newImgThumb.contentMode = .ScaleAspectFit 

希望你明白了!

將單元格圖像視圖的內容模式設置為scaleAspectFit

cell.imageView?.image = UIImage(named: weatherObject.icon)
cell.imageView?.frame = CGRect(x: 0, y: 0, width: 50, height: 50)
cell.imageView?.contentMode = .scaleAspectFit
cell.imageView?.clipsToBounds = true  // clipping will help check actual frame of image
cell.layoutIfNeeded()
//cell.setNeedsDisplay()  // try to reset display, if not updating your image scale automatically

一個不錯的建議, 使用“自定義表格”單元格並根據您的選擇/要求設置所有元素。

這是自定義Tableview單元的參考教程 讓我知道,如果您遇到自定義Tableview單元的任何問題。

自定義tableview單元,可根據您的需要靈活地安排所有單元元素。

暫無
暫無

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

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