简体   繁体   English

阴影没有出现 Swift 4.2

[英]Shadow is not appearing Swift 4.2

I'm trying to add a drop shadow to a button with swift.我正在尝试快速为按钮添加阴影。 I have created the elements on this view controller programmatically and I have a feeling this is why the shadow isn't appearing because the shadows appear in my other view controllers that I have in the app.我以编程方式在这个视图控制器上创建了元素,我有一种感觉,这就是为什么没有出现阴影的原因,因为阴影出现在我在应用程序中的其他视图控制器中。 I've also tried to deal with clipToBounds and maskToBounds but couldn't fix it.我也尝试过处理 clipToBounds 和 maskToBounds 但无法修复它。 What am I missing?我错过了什么?

Here is the code I'm using to try and get the shadow to appear.这是我用来尝试显示阴影的代码。

let dateLabelButton: UIButton = {
    let button = UIButton()
    button.translatesAutoresizingMaskIntoConstraints = false

    button.layer.cornerRadius = 10
    button.backgroundColor = Colours().brightRedColour

    button.addTarget(self, action: #selector(segueToPopUp), for: .touchUpInside)

    let shadow = UIBezierPath(roundedRect: button.bounds, cornerRadius: 10).cgPath
    button.layer.shadowRadius = 5
    button.layer.shadowColor = UIColor.black.cgColor
    button.layer.shadowOpacity = 1
    button.layer.masksToBounds = false
    button.layer.shadowPath = shadow

    return button
}()

Here are the constraints that I've added to this button.这是我添加到此按钮的约束。

// Sets up layout for date label button
    dateLabelButton.bottomAnchor.constraint(equalTo: self.view.topAnchor, constant: self.view.frame.height * 2/3 - 50).isActive = true
    dateLabelButton.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 20).isActive = true
    dateLabelButton.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -20).isActive = true
    dateLabelButton.topAnchor.constraint(equalTo: barChart.bottomAnchor, constant: 10).isActive = true
    dateLabelButton.heightAnchor.constraint(equalToConstant: 40).isActive = true

Any help would be great, I've been trying this for hours now and I just can't figure it out.任何帮助都会很棒,我已经尝试了几个小时,但我无法弄清楚。

If you're using UIBezierPath for shadow, you need to do it inside the viewDidLayoutSubviews() , like so:如果您使用UIBezierPath作为阴影,则需要在viewDidLayoutSubviews() ,如下所示:

override func viewDidLayoutSubviews() {
    super.viewDidLayoutSubviews()

    let shadow = UIBezierPath(roundedRect: dateLabelButton.bounds, cornerRadius: 10).cgPath
    dateLabelButton.layer.shadowRadius = 5
    dateLabelButton.layer.shadowColor = UIColor.black.cgColor
    dateLabelButton.layer.shadowOpacity = 1
    dateLabelButton.layer.masksToBounds = false
    dateLabelButton.layer.shadowPath = shadow
}

Otherwise, you can just comment out your shadowPath just like Enea's answer.否则,您可以像 Enea 的回答一样注释掉您的shadowPath

I think this will work.我认为这会奏效。

let dateLabelButton: UIButton = {
    let button = UIButton()
    button.translatesAutoresizingMaskIntoConstraints = false

    button.layer.cornerRadius = 10
    button.backgroundColor = Colours().brightRedColour

    button.addTarget(self, action: #selector(segueToPopUp), for: .touchUpInside)

    //let shadow = UIBezierPath(roundedRect: button.bounds, cornerRadius: 10).cgPath
    button.layer.shadowRadius = 5
    button.layer.shadowColor = UIColor.black.cgColor
    button.layer.shadowOffset = CGSize(width: 0.0, height: 2.0)
    button.layer.shadowOpacity = 1.0
    button.layer.masksToBounds = false
   // button.layer.shadowPath = shadow

    return button
}()

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

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