2017-06-05 78 views
1

我有一個自定義UITableViewCell與自我大小和UILabel和陰影。自動調整大小UITableViewCell陰影關閉

class CustomCell: UITableViewCell { 

    @IBOutlet weak var titleLabel: UILabel! 
    @IBOutlet weak var cellBackgroundView: UIView! 
    @IBOutlet weak var creationDateLabel: UILabel! 

    override func layoutSubviews() { 
    super.layoutSubviews() 
    self.cellBackgroundView.layer.cornerRadius = 12 

    self.cellBackgroundView.layer.shadowColor = UIColor.black.withAlphaComponent(1.0).cgColor 
    self.cellBackgroundView.layer.shadowOffset = CGSize.zero 
    self.cellBackgroundView.layer.shadowRadius = 9 
    self.cellBackgroundView.layer.shadowOpacity = 0.8 
    self.cellBackgroundView.layer.shadowPath = UIBezierPath(rect: self.cellBackgroundView.bounds).cgPath 
    self.cellBackgroundView.layer.masksToBounds = false 
    self.cellBackgroundView.layer.shouldRasterize = true 

    self.cellBackgroundView.layer.rasterizationScale = UIScreen.main.scale 
    } 
} 

我試圖實現layoutSubviews()cellForRowAt: indexPathwillDisplay cell這個代碼,但它始終關閉,而不是居中:

Shadow is not centered but off 什麼來實現它的正確方法?

編輯:它在我開始在tableView中滾動時起作用。

編輯2:這是應該的樣子:

This is how it should look!

謝謝!

+0

什麼是「永遠關閉而不居中」? – Koen

+0

@Koen你可以在我附上的圖片中看到它。陰影應該直接在單元格下面。我會張貼它應該看起來的照片。 – kuklis

回答

2

嘗試你的代碼發佈行:

self.cellBackgroundView.layer.shadowPath = UIBezierPath(rect: self.cellBackgroundView.bounds).cgPath 

這應該做的伎倆。

+0

這很甜!作品! – RaffAl