2

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

ShadeToD
  • 403
  • 9
  • 22
  • Hi! Seems the problem is not with shadowing. I suppose that you have some heavy function that is executing in main thread. Could you provide more info about what is going on in your ViewController. – Roman Tsymbaliuk Mar 21 '19 at 10:01
  • I am using mapKit and showing user Location on the map. This main view controller is using map and this bottom View with a button (blue bar) to stop showing location. – ShadeToD Mar 21 '19 at 10:22
  • ok. Do you have some messages about layout issues in Debug terminal? I've created similar functionality and have no issue. – Roman Tsymbaliuk Mar 21 '19 at 10:33
  • no errors or issues in terminal. No idea why it's happening. – ShadeToD Mar 21 '19 at 10:41
  • Sorry but without more details (source code) it is hard to find the issue. You can try step by step disable functionality of the ViewController and investigate the state of issue. – Roman Tsymbaliuk Mar 21 '19 at 10:52
  • So i decided to create UIView in new blank project. I added the same shadowpath as i mentioned earlier and the problem is still there. When u rotate device many times this glitchy shadow appers. I think its problem with UIBezierPath – ShadeToD Mar 21 '19 at 12:00
  • @RomanTsymbaliuk checkout my Edit, i added 2 screens and code. Problem still exists – ShadeToD Mar 21 '19 at 12:08

1 Answers1

0

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.

Roman Tsymbaliuk
  • 311
  • 2
  • 11