I have a View that contains other views. ViewforShadow -> View-> bottomBlueView
func setupMyView() {
myView.layer.shadowColor = #colorLiteral(red: 0.2901960784, green: 0.2901960784, blue: 0.2901960784, alpha: 1)
myView.layer.shadowOffset = CGSize.zero
myView.layer.shadowRadius = 4
myView.layer.shadowOpacity = 1
myView.layer.masksToBounds = false
myView.layer.shadowPath = UIBezierPath(roundedRect: myView.bounds, cornerRadius: myView.frame.height / 10).cgPath
myView.layer.backgroundColor = UIColor.clear.cgColor
}
Whenever i rotate my device i get this weird looking shadow
I understand that this problem occur because shadow has myView.bounds for portrait / landscape mode. So i need to change it whenever i rotate my device. I found viewWillLayoutSubviews() and viewDidLayoutSubviews() are great functions to solve my problem.
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
//update shadow
myView.layer.shadowPath = UIBezierPath(roundedRect: myView.bounds, cornerRadius: myView.frame.height / 10).cgPath
}
override func viewWillLayoutSubviews() {
super.viewWillLayoutSubviews()
//update shadow
myView.layer.shadowPath = UIBezierPath(roundedRect: myView.bounds, cornerRadius: myView.frame.height / 10).cgPath
}
I tried the both and it works fine , but whenever i rotate my device for like 1 second there's this glitchy shadow ( i don't like it). After 1 seconds
EDIT I decided to create new blank project and see why it's no't working. I Have 1 UIView inside my View controller and this code.
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var myView: UIView!
override func viewDidLoad() {
super.viewDidLoad()
setupMyView()
}
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
//update shadow
myView.frame = myView.frame
myView.layer.shadowPath = UIBezierPath(roundedRect: myView.bounds, cornerRadius: myView.frame.height / 10).cgPath
}
func setupMyView() {
myView.layer.shadowColor = #colorLiteral(red: 0.2745098174, green: 0.4862745106, blue: 0.1411764771, alpha: 1)
myView.layer.shadowOffset = CGSize.zero
myView.layer.shadowRadius = 4
myView.layer.shadowOpacity = 1
myView.layer.masksToBounds = false
myView.layer.shadowPath = UIBezierPath(roundedRect: myView.bounds, cornerRadius: myView.frame.height / 10).cgPath
// myView.layer.backgroundColor = UIColor.clear.cgColor
}
}
When u rotate device the shadow is not in correct position.