【问题标题】:Resizing icon using imageView使用 imageView 调整图标大小
【发布时间】:2017-12-26 10:45:07
【问题描述】:

我有以下代码(更新):

    // 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 中的值如何,“图标”的大小都保持不变。

任何指针,替代品将不胜感激!

提前致谢。

【问题讨论】:

  • 尝试添加等于50的高度和宽度约束
  • UIImageView 具有 contentMode 属性,您可以根据需要设置图像内容

标签: ios swift uitableview swift3 uiimageview


【解决方案1】:

您应该正确设置图像的内容模式,以便将它们缩放到正确的大小。

cell.imageView.contentMode = .scaleAspectFit

【讨论】:

    【解决方案2】:

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

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

    希望你明白了!!!

    【讨论】:

    • Bhai,问题 Swift 3 ma 6,芋头回答 Swift 2 ma.. Ene Swift 3 ma convert kar.
    【解决方案3】:

    设置单元格图片视图的内容模式为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
    

    给您一个很好的建议,使用自定义表格视图单元格并根据您的选择/要求设置所有元素。

    Here is reference tutorial for Custom Tableview cell。如果您在使用自定义 Tableview 单元时遇到任何问题,请告诉我。

    自定义表格视图单元格,可根据您的需要灵活排列所有单元格元素。

    【讨论】:

    • 感谢您的回复,克鲁纳尔。不幸的是,图标仍然没有调整大小。我已经更新了上面的代码。也许有人可以为我指出另一个方向?
    • @J. Dorais,给你一个很好的建议,使用自定义表格视图单元格并根据您的选择/要求设置所有元素。如果您在使用自定义 Tableview 单元时遇到任何问题,请告诉我。
    猜你喜欢
    • 1970-01-01
    • 2013-02-02
    • 1970-01-01
    • 2013-06-27
    • 2018-02-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多