ios - 在自定义 UITableViewCell 中设置元素的角半径

标签 ios swift uitableview

我的代码几乎可以正常工作,但有一个问题。

表格加载完毕后,图像如下所示:

As soon as the table is loaded, images looks are as follows

但是当我滚动表格时,一切都很好:

But when i scroll the table everything is good

这里缺少什么?

class ShopTableViewCell: UITableViewCell {

let disposeBag = DisposeBag()

@IBOutlet weak var shopImageView: UIImageView!
@IBOutlet weak var imagesCollectionView: UICollectionView!
@IBOutlet weak var nameLabel: UILabel!
@IBOutlet weak var likeButton: UIButton!
@IBOutlet weak var imageContainer: UIView!

override func layoutSubviews() {
    super.layoutSubviews()
    imageContainer.clipsToBounds = true
    imageContainer.layer.cornerRadius = imageContainer.frame.size.width / 2
    imageContainer.layer.borderWidth = 1
    imageContainer.layer.borderColor = UIColor(hex: "#BDBDBD").CGColor

最佳答案

这是因为您的 imageview 的 frame 属性尚未更新。

你可以创建一个 UIImageView 的子类并在那里完成那个工作, 喜欢:

swift

class CircleImageView: UIImageView {

  override func layoutSubviews() {
    super.layoutSubviews()

    self.layer.borderWidth = 1 / UIScreen.mainScreen().scale
    self.layer.borderColor = UIColor(hex: "#BDBDBD").CGColor
    self.layer.cornerRadius = self.bounds.size.width/2
    self.layer.masksToBounds = true
  }
}

objective-C

@implementation CircleImageView

- (void)layoutSubviews {
    [super layoutSubviews];

    self.layer.borderWidth = 1 / [UIScreen mainScreen].scale;
    self.layer.borderColor = [UIColor whiteColor].CGColor;
    self.layer.cornerRadius = self.bounds.size.width/2;
    self.layer.masksToBounds = YES;
}

@end

关于ios - 在自定义 UITableViewCell 中设置元素的角半径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39319306/

相关文章:

iphone - ELCImagePickerController 的 Indicatorview

iOS 应用负徽号?

ios - Swift:如何使用 Sprite 套件游戏实现额外的 View Controller ?

ios - 如何在 swift 中为标签栏角标(Badge)计数设置动画

swift ios 9 : Section header change position after reload data

ios - UITableViewCell没有显示标签

ios - 无法识别 UIImageWriteToSavedPhotosAlbum 中的 Swift 选择器,为什么?

ios - 如何在 UITextView 中单个段落的左侧添加装饰器?

iphone - UITableViewCell 的默认字体大小

iphone - 具有不同 TableView 标签的 TableView 的问题