首页 > 解决方案 > 添加到创建的视图的阴影没有出现

问题描述

我正在以编程方式添加 UIView,并使用 NSLayoutConstraint 设置其约束,如下所示,但没有添加阴影。

对于阴影,我使用的是 SwifterSwift .addShadow()

我得到的结果

界面视图:

    lazy var alertViewNew: UIView = {
        let view = UIView()
        view.layer.zPosition = 1
        view.cornerRadius = 20
        view.addShadow(ofColor: .lightGray, radius: 3, offset: .zero, opacity: 0.3)
        view.translatesAutoresizingMaskIntoConstraints = false
        
        return alertView
    }()

添加约束

func setUpAlertView() {
                
        [alertViewNew].forEach {
            (view.addSubview($0))
        }

        NSLayoutConstraint.activate([
        
            alertViewNew.centerYAnchor.constraint(equalTo: view.centerYAnchor),
            alertViewNew.centerXAnchor.constraint(equalTo: view.centerXAnchor),
            alertViewNew.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 10),
            alertViewNew.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -10),
            
            titleLabel.leadingAnchor.constraint(equalTo: alertViewNew.leadingAnchor, constant: 20),
            titleLabel.trailingAnchor.constraint(equalTo: alertViewNew.trailingAnchor, constant: -20),
            titleLabel.topAnchor.constraint(equalTo: alertViewNew.topAnchor, constant: 20),
            
            descriptionLabel.leadingAnchor.constraint(equalTo: alertViewNew.leadingAnchor, constant: 20),
            descriptionLabel.trailingAnchor.constraint(equalTo: alertViewNew.trailingAnchor, constant: -20),
            descriptionLabel.topAnchor.constraint(equalTo: titleLabel.bottomAnchor, constant: 20),
            
            updateButton.topAnchor.constraint(equalTo: descriptionLabel.bottomAnchor, constant: 5),
            updateButton.trailingAnchor.constraint(equalTo: alertViewNew.trailingAnchor, constant: -20),
            updateButton.widthAnchor.constraint(equalToConstant: 65),
            updateButton.bottomAnchor.constraint(equalTo: alertViewNew.bottomAnchor, constant: -20),
        ])
    }

Swifter Swift 的 AddShadow

    func addShadow(ofColor color: UIColor = UIColor(red: 0.07, green: 0.47, blue: 0.57, alpha: 1.0), radius: CGFloat = 3, offset: CGSize = .zero, opacity: Float = 0.5) {
        layer.shadowColor = color.cgColor
        layer.shadowOffset = offset
        layer.shadowRadius = radius
        layer.shadowOpacity = opacity
        layer.masksToBounds = false
    }

我试图解决问题的事情

将掩码设置为绑定为真

设置对 true 不透明

以及在 stackoverflow 上发现的其他一些试验

这些都不起作用

标签: swiftuiviewuikitlayershadow

解决方案


这有点难以帮助,因为您显示的代码不完整(并且有错误,如所写)。

例如,我假设您func addShadowUIView扩展名如下:

extension UIView {
    func addShadow(ofColor color: UIColor = UIColor(red: 0.07, green: 0.47, blue: 0.57, alpha: 1.0), radius: CGFloat = 3, offset: CGSize = .zero, opacity: Float = 0.5) {
        layer.shadowColor = color.cgColor
        layer.shadowOffset = offset
        layer.shadowRadius = radius
        layer.shadowOpacity = opacity
        
        // no need for this
        //layer.masksToBounds = false
    }
}

接下来,您的lazy var alertViewNew

lazy var alertViewNew: UIView = {
    let view = UIView()
    
    // no logical reason for this
    //view.layer.zPosition = 1
    
    // .cornerRadius is not a property of `UIView`
    //view.cornerRadius = 20
    
    // assuming this is in a UIView extension
    view.addShadow(ofColor: .lightGray, radius: 3, offset: .zero, opacity: 0.3)
    
    view.translatesAutoresizingMaskIntoConstraints = false

    // no such thing as alertView
    //return alertView
    
    return view
}()

但是...如果我们假设您的代码确实有效(标签在某处定义和创建,子视图被正确添加等),您没有看到阴影的最可能原因是因为您alertViewNew可能有清晰的背景。如果很清楚,那就没有什么可以“投下阴影”了。

尝试设置alertViewNew.backgroundColor = .white,看看是否可以解决问题。

或者,试试这个工作示例:

class CustomAlertTestVC: UIViewController {
    
    lazy var alertViewNew: UIView = {
        let view = UIView()
        view.translatesAutoresizingMaskIntoConstraints = false
        return view
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()

        view.backgroundColor = .white
        
        view.addSubview(alertViewNew)
        setUpAlertView()

    }
    
    func setUpAlertView() {

        let titleLabel = UILabel()
        let descriptionLabel = UILabel()
        let updateButton = UIButton()

        titleLabel.font = .boldSystemFont(ofSize: 16.0)
        titleLabel.text = "New Version Available"
        
        descriptionLabel.font = .systemFont(ofSize: 16.0)
        descriptionLabel.numberOfLines = 0
        descriptionLabel.text = "Please, Update application to the new version to continue."
        
        updateButton.setTitle("UPDATE", for: [])
        updateButton.setTitleColor(.systemBlue, for: [])

        [titleLabel, descriptionLabel, updateButton].forEach {
            $0.translatesAutoresizingMaskIntoConstraints = false
            alertViewNew.addSubview($0)
        }
        
        NSLayoutConstraint.activate([
            
            alertViewNew.centerYAnchor.constraint(equalTo: view.centerYAnchor),
            // no need for centerX since we're adding leading and trailing constraints
            //alertViewNew.centerXAnchor.constraint(equalTo: view.centerXAnchor),
            alertViewNew.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 10),
            alertViewNew.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -10),
            
            titleLabel.leadingAnchor.constraint(equalTo: alertViewNew.leadingAnchor, constant: 20),
            titleLabel.trailingAnchor.constraint(equalTo: alertViewNew.trailingAnchor, constant: -20),
            titleLabel.topAnchor.constraint(equalTo: alertViewNew.topAnchor, constant: 20),
            
            descriptionLabel.leadingAnchor.constraint(equalTo: alertViewNew.leadingAnchor, constant: 20),
            descriptionLabel.trailingAnchor.constraint(equalTo: alertViewNew.trailingAnchor, constant: -20),
            descriptionLabel.topAnchor.constraint(equalTo: titleLabel.bottomAnchor, constant: 20),
            
            updateButton.topAnchor.constraint(equalTo: descriptionLabel.bottomAnchor, constant: 5),
            updateButton.trailingAnchor.constraint(equalTo: alertViewNew.trailingAnchor, constant: -20),
            // really no need for width constraint
            //updateButton.widthAnchor.constraint(equalToConstant: 65),
            updateButton.bottomAnchor.constraint(equalTo: alertViewNew.bottomAnchor, constant: -20),
            
        ])
        
        alertViewNew.layer.shadowColor = UIColor.lightGray.cgColor
        alertViewNew.layer.shadowOffset = .zero
        alertViewNew.layer.shadowRadius = 3.0
        alertViewNew.layer.shadowOpacity = 0.3
        alertViewNew.layer.cornerRadius = 20.0
        
        // to get the view's layer to "cast a shadow"
        
        // either set the view's backgroundColor
        alertViewNew.backgroundColor = .white
        
        // or, set the layer's backgroundColor
        //alertViewNew.layer.backgroundColor = UIColor.white.cgColor

    }

}

输出:

在此处输入图像描述


推荐阅读