gpt4 book ai didi

swift - UIView 阴影不可见

转载 作者:搜寻专家 更新时间:2023-11-01 06:53:01 24 4
gpt4 key购买 nike

我有一个名为 containerViewUIView

在创建 UIView 并将其添加到主视图后,我试图添加一点阴影。我的代码:

    let containerView = UIView()

let bg_clear = UIColor(hexString: "#34495E")
containerView.backgroundColor = bg_clear


containerView.layer.cornerRadius = 15
containerView.clipsToBounds = false

containerView.dropShadow() // Generate Shadow

view.addSubview(containerView)

containerView.translatesAutoresizingMaskIntoConstraints = false
containerView.heightAnchor.constraint(equalToConstant: 50).isActive = true
containerView.widthAnchor.constraint(equalToConstant: 300).isActive = true
containerView.centerXAnchor.constraint(equalTo: tabBar.centerXAnchor).isActive = true

生成阴影的代码:

extension UIView {

func dropShadow(scale: Bool = true) {
layer.masksToBounds = false
layer.shadowColor = UIColor(hexString: "#000000").cgColor
layer.shadowOpacity = 0.5
layer.shadowOffset = CGSize(width: -1, height: 1)
layer.shadowRadius = 1

layer.shadowPath = UIBezierPath(rect: bounds).cgPath
layer.shouldRasterize = true
layer.rasterizationScale = scale ? UIScreen.main.scale : 1
}
}

最佳答案

这应该有所帮助:

extension UIView {

func addShadow(withOpacity opacity:Float, radius:CGFloat, andColor color:UIColor) {
self.layer.shadowColor = color.cgColor
self.layer.shadowOffset = CGSize(width:-1.0, height:1.0)
self.layer.shadowOpacity = opacity
self.layer.shadowRadius = radius
}
}

示例:

containerView.addShadow(withOpacity: 0.4, radius: 0.4, andColor: .black)

关于swift - UIView 阴影不可见,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55868518/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com