Update shadow path on device rotation change. Problem with shadow

1k views Asked by At

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

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. shadowproblem

after 1 seconds its look good (normal state) normalstate

1

There are 1 answers

0
Roman Tsymbaliuk On

You can try to setup ViewForShadow in a such way:

in ViewController:

    fileprivate func setupMyView() {
        viewForShadow.layer.shadowColor = #colorLiteral(red: 0.2901960784, green: 0.2901960784, blue: 0.2901960784, alpha: 1)
        viewForShadow.layer.shadowOffset = CGSize.zero
        viewForShadow.layer.shadowRadius = 4
        viewForShadow.layer.shadowOpacity = 1
        viewForShadow.backgroundColor = .clear
    }

As you see without shadowPath.

WhiteView (view that contains bottomBlueView) will have some setups of corner radius:

    fileprivate func setupWhiteView() {
        whiteView.layer.cornerRadius = 5.0
        whiteView.layer.masksToBounds = true
    }

That should be enough.