简体   繁体   English

单元格图像视图cornerRadius迅速

[英]Cell image view cornerRadius in swift

Where would I best set the cornerRadius property? 我最好在哪里设置cornerRadius属性?

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    return 1/6 * tableView.bounds.width
}

I have tried it in the cell's awakeFromNib, but it doesn't yet seem to have the proper height. 我已经在单元的awakeFromNib中尝试过了,但是它似乎还没有合适的高度。

If the image view size if flexible and the corner radius is linked to the image size, I advise you to set the corner radius in the layoutSubviews() method of your cell. 如果图像视图的大小(如果灵活)并且拐角半径与图像大小相关联,则建议您在单元格的layoutSubviews()方法中设置拐角半径。

You can set it as following: 您可以将其设置如下:

override func layoutSubviews() {
    super.layoutSubviews()
    imageView.layer.cornerRadius = imageView.frame.size.width/2.0
}

Okay warning - all untested code 好的警告-所有未经测试的代码

If you assume that the object which knows best how to draw a cell, is the cell itself.... 如果假设最了解如何绘制单元格的对象是单元格本身。

Create a subclass of the Cell 创建单元的子类

class MyUITableViewCell: UITableViewCell

Now, there are many ways to skin this cell. 现在,有很多方法可以使该单元格皮肤化。 My preference is to: 我的偏好是:

Add a method for all of the little things you wish to change in the cell 为您要在单元格中更改的所有小事情添加方法

func configure () {

    self.layer.cornerRadius = 5.0 // for instance

}

When should you call configure? 您什么时候应该打电话给configure?

Arguably you could call in in draw() but I like to 可以说您可以在draw()中调用,但是我喜欢

Call configure() via the delegate 通过委托调用configure()

func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {

    if cell.responds(to: #selector(MyUITableViewCell.configure()) {

        cell.configure()

    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在表格视图单元格内的图像视图上设置cornerRadius - How to set cornerRadius on a imageview inside a table view cell view.Layer.CornerRadius 不适用于 UIView Swift 3 iOS 的 UIScrollView 子视图 - view.Layer.CornerRadius not working on UIScrollView subView of UIView Swift 3 iOS 表视图细胞迅速的圆形图像 - circular image in table view cell swift iOS如何在表格视图单元格Swift中调整图像大小 - iOS How to resize image at table view cell Swift 在swift3的表格视图的每个单元格中重复出现图像 - Image repeates in every cell of table view in swift3 iOS如何从表视图单元格Swift中的解析中下载图像 - iOS How to download image from parse in table view cell Swift Swift - CompositionalLayout - 集合视图单元格高度未根据图像高度计算 - Swift - CompositionalLayout - collection view cell height not calculated based on Image height 使用Swift 2.0将Tableview单元格图像转移到另一个视图 - Transfer Tableview cell image to another view using swift 2.0 SWIFT - 将 layer.cornerRadius 设置为大于视图高度会导致问题 - SWIFT - Setting layer.cornerRadius greater than the view height causes issues 快速更改弹出视图控制器cornerRadius - change popover viewcontroller cornerRadius in swift
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM