影子没有显示uistackview



我试图将阴影送给uistackView。我的stackView是在具有一些标签和按钮的原型元中。首先,我已经在bonded to to My自定义单元格的一类中用名称 uiviewBank 引用了stackView:

class CustChatBankCell: UITableViewCell {
    @IBOutlet weak var UIViewBank: UIStackView!
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }
    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)
        // Configure the view for the selected state
    }
}

在我的控制器中,我使用的是:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell:CustChatBankCell = self.tbChat.dequeueReusableCell(withIdentifier: "CustomBankCell") as! CustChatBankCell
                    cell.UIViewBank.dropShadow()
                    return cell
                }
}

扩展代码:

extension UIStackView {
    func dropShadow(scale: Bool = true) {
        self.layer.masksToBounds = false
        self.layer.shadowColor = UIColor.black.cgColor
        self.layer.shadowOpacity = 0.5
        self.layer.shadowOffset = CGSize(width: -1, height: 501)
        self.layer.shadowRadius = 1
        self.layer.shadowPath = UIBezierPath(rect: self.bounds).cgPath
        self.layer.shouldRasterize = true
        self.layer.rasterizationScale = scale ? UIScreen.main.scale : 1
    }
}

,但阴影没有出现。

您无法执行此操作,因为UIStackView是一种非绘制视图,这意味着drawRect()从未被调用,并且其背景颜色被忽略。考虑将UIStackView放置在另一个UIView中,并给出该视图的背景颜色。

来自Apple Docs

uistackView是 uiview ;也就是说,它不提供其自己的任何用户界面。取而代之的是,它仅管理其布置视图的位置和大小。结果,某些属性(喜欢 背景颜色 )对堆栈视图没有影响。同样,你不能覆盖 外行 ,,,, 绘制( :) ,或绘制(:in:)。

最新更新