简体   繁体   English

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

[英]Setting corner radius of an element in a custom UITableViewCell

My code is almost working, but there is one issue. 我的代码几乎正常工作,但有一个问题。

As soon as the table is loaded, images looks are as follows: 加载表后,图像看起来如下:

加载表后,图像看起来如下

But when i scroll the table everything is good: 但是当我滚动表格时,一切都很好:

但是当我滚动桌子时,一切都很好

What is missing here? 这里缺少什么?

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

It's because the frame property of your imageview is not updated yet. 这是因为您的imageview的frame属性尚未更新。

You can make a subclass of UIImageView and do that job there, like: 您可以创建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 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

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM