Multiple shadows under UIView iOS Swift The Next CEO of Stack OverflowHow do I draw a shadow under a UIView?Giving UIView rounded cornersUIView with rounded corners and drop shadow?UIButton w/ gradient, rounded corners, border, and drop shadowCALayer's backgroundFilters doesn nothing on UIViewHow to call Objective-C code from Swift#pragma mark in Swift?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?

What benefits would be gained by using human laborers instead of drones in deep sea mining?

Novel about a guy who is possessed by the divine essence and the world ends?

Sending manuscript to multiple publishers

Would a completely good Muggle be able to use a wand?

"In the right combination" vs "with the right combination"?

Written every which way

In excess I'm lethal

How do I make a variable always equal to the result of some calculations?

Which tube will fit a -(700 x 25c) wheel?

How do scammers retract money, while you can’t?

If Nick Fury and Coulson already knew about aliens (Kree and Skrull) why did they wait until Thor's appearance to start making weapons?

Received an invoice from my ex-employer billing me for training; how to handle?

MessageLevel in QGIS3

How do I go from 300 unfinished/half written blog posts, to published posts?

If/When UK leaves the EU, can a future goverment conduct a referendum to join the EU?

Real integral using residue theorem - why doesn't this work?

Can I equip Skullclamp on a creature I am sacrificing?

Is it ever safe to open a suspicious html file (e.g. email attachment)?

Return the Closest Prime Number

What does convergence in distribution "in the Gromov–Hausdorff" sense mean?

Why do professional authors make "consistency" mistakes? And how to avoid them?

Why don't programming languages automatically manage the synchronous/asynchronous problem?

How do we know the LHC results are robust?

Can I run my washing machine drain line into a condensate pump so it drains better?



Multiple shadows under UIView iOS Swift



The Next CEO of Stack OverflowHow do I draw a shadow under a UIView?Giving UIView rounded cornersUIView with rounded corners and drop shadow?UIButton w/ gradient, rounded corners, border, and drop shadowCALayer's backgroundFilters doesn nothing on UIViewHow to call Objective-C code from Swift#pragma mark in Swift?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?










1















I am trying to make a UIButton with rounded corners that has 2 colored shadows. Why is the red (and at this point also the blue "shadow" layer covering the button? How to get the shadows below the button canvas). I thought it was helping to insert sublayers instead of just adding them.



Shadows are above button



I have made a playground illustrating the issue



import UIKit
import PlaygroundSupport


This is the button im trying to implement



class PrimaryButton: UIButton 
required init(text: String = "Test 1", hasShadow: Bool = true)
super.init(frame: .zero)
setTitle(text, for: .normal)
backgroundColor = UIColor.blue
layer.cornerRadius = 48 / 2
layer.masksToBounds = false
if hasShadow
insertShadow()



fileprivate func insertShadow()
let layer2 = CALayer(layer: layer), layer3 = CALayer(layer: layer)
layer2.applySketchShadow(color: UIColor.red, alpha: 0.5, x: 0, y: 15, blur: 35, spread: -10)
layer3.applySketchShadow(color: UIColor.blue, alpha: 0.5, x: 0, y: 10, blur: 21, spread: -9)
layer.insertSublayer(layer2, at: 0)
layer.insertSublayer(layer3, at: 0)


required init?(coder aDecoder: NSCoder)
fatalError("init(coder:) has not been implemented")


override func layoutSubviews()
super.layoutSubviews()
layer.sublayers?.forEach (sublayer) in
sublayer.shadowPath = UIBezierPath(rect: bounds).cgPath





This is an extension that helps adding the shadow from Sketch specification:



extension CALayer 
func applySketchShadow(
color: UIColor = .black,
alpha: Float = 0.5,
x: CGFloat = 0,
y: CGFloat = 2,
blur: CGFloat = 4,
spread: CGFloat = 0)

shadowColor = color.cgColor
shadowOpacity = alpha
shadowOffset = CGSize(width: x, height: y)
shadowRadius = blur / 2.0
if spread == 0
shadowPath = nil
else
let dx = -spread
let rect = bounds.insetBy(dx: dx, dy: dx)
shadowPath = UIBezierPath(rect: rect).cgPath

masksToBounds = false




class MyViewController : UIViewController
override func loadView()
let view = UIView()
view.backgroundColor = .white

let button = PrimaryButton()
button.frame = CGRect(x: 150, y: 200, width: 200, height: 48)
view.addSubview(button)
self.view = view


// Present the view controller in the Live View window
PlaygroundPage.current.liveView = MyViewController()









share|improve this question


























    1















    I am trying to make a UIButton with rounded corners that has 2 colored shadows. Why is the red (and at this point also the blue "shadow" layer covering the button? How to get the shadows below the button canvas). I thought it was helping to insert sublayers instead of just adding them.



    Shadows are above button



    I have made a playground illustrating the issue



    import UIKit
    import PlaygroundSupport


    This is the button im trying to implement



    class PrimaryButton: UIButton 
    required init(text: String = "Test 1", hasShadow: Bool = true)
    super.init(frame: .zero)
    setTitle(text, for: .normal)
    backgroundColor = UIColor.blue
    layer.cornerRadius = 48 / 2
    layer.masksToBounds = false
    if hasShadow
    insertShadow()



    fileprivate func insertShadow()
    let layer2 = CALayer(layer: layer), layer3 = CALayer(layer: layer)
    layer2.applySketchShadow(color: UIColor.red, alpha: 0.5, x: 0, y: 15, blur: 35, spread: -10)
    layer3.applySketchShadow(color: UIColor.blue, alpha: 0.5, x: 0, y: 10, blur: 21, spread: -9)
    layer.insertSublayer(layer2, at: 0)
    layer.insertSublayer(layer3, at: 0)


    required init?(coder aDecoder: NSCoder)
    fatalError("init(coder:) has not been implemented")


    override func layoutSubviews()
    super.layoutSubviews()
    layer.sublayers?.forEach (sublayer) in
    sublayer.shadowPath = UIBezierPath(rect: bounds).cgPath





    This is an extension that helps adding the shadow from Sketch specification:



    extension CALayer 
    func applySketchShadow(
    color: UIColor = .black,
    alpha: Float = 0.5,
    x: CGFloat = 0,
    y: CGFloat = 2,
    blur: CGFloat = 4,
    spread: CGFloat = 0)

    shadowColor = color.cgColor
    shadowOpacity = alpha
    shadowOffset = CGSize(width: x, height: y)
    shadowRadius = blur / 2.0
    if spread == 0
    shadowPath = nil
    else
    let dx = -spread
    let rect = bounds.insetBy(dx: dx, dy: dx)
    shadowPath = UIBezierPath(rect: rect).cgPath

    masksToBounds = false




    class MyViewController : UIViewController
    override func loadView()
    let view = UIView()
    view.backgroundColor = .white

    let button = PrimaryButton()
    button.frame = CGRect(x: 150, y: 200, width: 200, height: 48)
    view.addSubview(button)
    self.view = view


    // Present the view controller in the Live View window
    PlaygroundPage.current.liveView = MyViewController()









    share|improve this question
























      1












      1








      1








      I am trying to make a UIButton with rounded corners that has 2 colored shadows. Why is the red (and at this point also the blue "shadow" layer covering the button? How to get the shadows below the button canvas). I thought it was helping to insert sublayers instead of just adding them.



      Shadows are above button



      I have made a playground illustrating the issue



      import UIKit
      import PlaygroundSupport


      This is the button im trying to implement



      class PrimaryButton: UIButton 
      required init(text: String = "Test 1", hasShadow: Bool = true)
      super.init(frame: .zero)
      setTitle(text, for: .normal)
      backgroundColor = UIColor.blue
      layer.cornerRadius = 48 / 2
      layer.masksToBounds = false
      if hasShadow
      insertShadow()



      fileprivate func insertShadow()
      let layer2 = CALayer(layer: layer), layer3 = CALayer(layer: layer)
      layer2.applySketchShadow(color: UIColor.red, alpha: 0.5, x: 0, y: 15, blur: 35, spread: -10)
      layer3.applySketchShadow(color: UIColor.blue, alpha: 0.5, x: 0, y: 10, blur: 21, spread: -9)
      layer.insertSublayer(layer2, at: 0)
      layer.insertSublayer(layer3, at: 0)


      required init?(coder aDecoder: NSCoder)
      fatalError("init(coder:) has not been implemented")


      override func layoutSubviews()
      super.layoutSubviews()
      layer.sublayers?.forEach (sublayer) in
      sublayer.shadowPath = UIBezierPath(rect: bounds).cgPath





      This is an extension that helps adding the shadow from Sketch specification:



      extension CALayer 
      func applySketchShadow(
      color: UIColor = .black,
      alpha: Float = 0.5,
      x: CGFloat = 0,
      y: CGFloat = 2,
      blur: CGFloat = 4,
      spread: CGFloat = 0)

      shadowColor = color.cgColor
      shadowOpacity = alpha
      shadowOffset = CGSize(width: x, height: y)
      shadowRadius = blur / 2.0
      if spread == 0
      shadowPath = nil
      else
      let dx = -spread
      let rect = bounds.insetBy(dx: dx, dy: dx)
      shadowPath = UIBezierPath(rect: rect).cgPath

      masksToBounds = false




      class MyViewController : UIViewController
      override func loadView()
      let view = UIView()
      view.backgroundColor = .white

      let button = PrimaryButton()
      button.frame = CGRect(x: 150, y: 200, width: 200, height: 48)
      view.addSubview(button)
      self.view = view


      // Present the view controller in the Live View window
      PlaygroundPage.current.liveView = MyViewController()









      share|improve this question














      I am trying to make a UIButton with rounded corners that has 2 colored shadows. Why is the red (and at this point also the blue "shadow" layer covering the button? How to get the shadows below the button canvas). I thought it was helping to insert sublayers instead of just adding them.



      Shadows are above button



      I have made a playground illustrating the issue



      import UIKit
      import PlaygroundSupport


      This is the button im trying to implement



      class PrimaryButton: UIButton 
      required init(text: String = "Test 1", hasShadow: Bool = true)
      super.init(frame: .zero)
      setTitle(text, for: .normal)
      backgroundColor = UIColor.blue
      layer.cornerRadius = 48 / 2
      layer.masksToBounds = false
      if hasShadow
      insertShadow()



      fileprivate func insertShadow()
      let layer2 = CALayer(layer: layer), layer3 = CALayer(layer: layer)
      layer2.applySketchShadow(color: UIColor.red, alpha: 0.5, x: 0, y: 15, blur: 35, spread: -10)
      layer3.applySketchShadow(color: UIColor.blue, alpha: 0.5, x: 0, y: 10, blur: 21, spread: -9)
      layer.insertSublayer(layer2, at: 0)
      layer.insertSublayer(layer3, at: 0)


      required init?(coder aDecoder: NSCoder)
      fatalError("init(coder:) has not been implemented")


      override func layoutSubviews()
      super.layoutSubviews()
      layer.sublayers?.forEach (sublayer) in
      sublayer.shadowPath = UIBezierPath(rect: bounds).cgPath





      This is an extension that helps adding the shadow from Sketch specification:



      extension CALayer 
      func applySketchShadow(
      color: UIColor = .black,
      alpha: Float = 0.5,
      x: CGFloat = 0,
      y: CGFloat = 2,
      blur: CGFloat = 4,
      spread: CGFloat = 0)

      shadowColor = color.cgColor
      shadowOpacity = alpha
      shadowOffset = CGSize(width: x, height: y)
      shadowRadius = blur / 2.0
      if spread == 0
      shadowPath = nil
      else
      let dx = -spread
      let rect = bounds.insetBy(dx: dx, dy: dx)
      shadowPath = UIBezierPath(rect: rect).cgPath

      masksToBounds = false




      class MyViewController : UIViewController
      override func loadView()
      let view = UIView()
      view.backgroundColor = .white

      let button = PrimaryButton()
      button.frame = CGRect(x: 150, y: 200, width: 200, height: 48)
      view.addSubview(button)
      self.view = view


      // Present the view controller in the Live View window
      PlaygroundPage.current.liveView = MyViewController()






      ios swift uiview calayer shadow






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Mar 21 at 17:05









      user317706user317706

      97031013




      97031013






















          3 Answers
          3






          active

          oldest

          votes


















          2














          It seems legit to me. layer1 & layer2 are sublayers of the button layer.



          You could add a third layer that will serve as a background. Here is an example based on your code:



          class PrimaryButton: UIButton 

          let layer1 = CALayer(), layer2 = CALayer(), layer3 = CALayer()

          override func layoutSubviews()
          super.layoutSubviews()
          layer1.backgroundColor = UIColor.blue.cgColor
          layer1.cornerRadius = 48 / 2
          [layer1, layer2, layer3].forEach
          $0.masksToBounds = false
          $0.frame = layer.bounds
          layer.insertSublayer($0, at: 0)

          layer2.applySketchShadow(color: UIColor.red, alpha: 0.5, x: 0, y: 15, blur: 35, spread: -10)
          layer3.applySketchShadow(color: UIColor.blue, alpha: 0.5, x: 0, y: 10, blur: 21, spread: -9)




          Note that I put most of the code inside layoutSubviews because most of your methods use the actual bounds of the button.






          share|improve this answer

























          • layer.masksToBounds = false is set 3 times? $0 or before forEach?

            – user317706
            Mar 21 at 21:07











          • @user317706 I edited the answer. You are right.

            – GaétanZ
            Mar 21 at 21:17


















          1














          Change your insertions to:



           layer.insertSublayer(layer2, at: 1)
          layer.insertSublayer(layer3, at: 2)


          That should do it.






          share|improve this answer























          • It did. Why? What is so special about the layer at index 0?

            – user317706
            Mar 21 at 20:36












          • I don't know why this works because everything I'm reading on z-ordering says it shouldn't and your original code should be correct. I just had a feeling and tried it :)

            – cbiggin
            Mar 21 at 20:56






          • 1





            It is not working. Take a look a the blue background side by side. There are not the same. You just switched the blue with the red.

            – GaétanZ
            Mar 21 at 21:28











          • Also had some trouble when implementing in my real solution. Have been looking at too many shadows today. The idea of having a background layer and 2 shadow layers worked fine.

            – user317706
            Mar 21 at 21:45


















          0














          Another way is to add double buttons without change your class.



           let button = PrimaryButton()
          button.frame = CGRect(x: 150, y: 200, width: 200, height: 48)
          button.backgroundColor = UIColor.clear
          view.addSubview(button)
          self.view = view
          let button1 = PrimaryButton()
          button1.frame = CGRect(x: 0, y: 0, width: 200, height: 48)
          button.addSubview(button1)
          button1.layer.sublayers?.forEach$0.removeFromSuperlayer()





          share|improve this answer























            Your Answer






            StackExchange.ifUsing("editor", function ()
            StackExchange.using("externalEditor", function ()
            StackExchange.using("snippets", function ()
            StackExchange.snippets.init();
            );
            );
            , "code-snippets");

            StackExchange.ready(function()
            var channelOptions =
            tags: "".split(" "),
            id: "1"
            ;
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function()
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled)
            StackExchange.using("snippets", function()
            createEditor();
            );

            else
            createEditor();

            );

            function createEditor()
            StackExchange.prepareEditor(
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader:
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            ,
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            );



            );













            draft saved

            draft discarded


















            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55285700%2fmultiple-shadows-under-uiview-ios-swift%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown

























            3 Answers
            3






            active

            oldest

            votes








            3 Answers
            3






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            2














            It seems legit to me. layer1 & layer2 are sublayers of the button layer.



            You could add a third layer that will serve as a background. Here is an example based on your code:



            class PrimaryButton: UIButton 

            let layer1 = CALayer(), layer2 = CALayer(), layer3 = CALayer()

            override func layoutSubviews()
            super.layoutSubviews()
            layer1.backgroundColor = UIColor.blue.cgColor
            layer1.cornerRadius = 48 / 2
            [layer1, layer2, layer3].forEach
            $0.masksToBounds = false
            $0.frame = layer.bounds
            layer.insertSublayer($0, at: 0)

            layer2.applySketchShadow(color: UIColor.red, alpha: 0.5, x: 0, y: 15, blur: 35, spread: -10)
            layer3.applySketchShadow(color: UIColor.blue, alpha: 0.5, x: 0, y: 10, blur: 21, spread: -9)




            Note that I put most of the code inside layoutSubviews because most of your methods use the actual bounds of the button.






            share|improve this answer

























            • layer.masksToBounds = false is set 3 times? $0 or before forEach?

              – user317706
              Mar 21 at 21:07











            • @user317706 I edited the answer. You are right.

              – GaétanZ
              Mar 21 at 21:17















            2














            It seems legit to me. layer1 & layer2 are sublayers of the button layer.



            You could add a third layer that will serve as a background. Here is an example based on your code:



            class PrimaryButton: UIButton 

            let layer1 = CALayer(), layer2 = CALayer(), layer3 = CALayer()

            override func layoutSubviews()
            super.layoutSubviews()
            layer1.backgroundColor = UIColor.blue.cgColor
            layer1.cornerRadius = 48 / 2
            [layer1, layer2, layer3].forEach
            $0.masksToBounds = false
            $0.frame = layer.bounds
            layer.insertSublayer($0, at: 0)

            layer2.applySketchShadow(color: UIColor.red, alpha: 0.5, x: 0, y: 15, blur: 35, spread: -10)
            layer3.applySketchShadow(color: UIColor.blue, alpha: 0.5, x: 0, y: 10, blur: 21, spread: -9)




            Note that I put most of the code inside layoutSubviews because most of your methods use the actual bounds of the button.






            share|improve this answer

























            • layer.masksToBounds = false is set 3 times? $0 or before forEach?

              – user317706
              Mar 21 at 21:07











            • @user317706 I edited the answer. You are right.

              – GaétanZ
              Mar 21 at 21:17













            2












            2








            2







            It seems legit to me. layer1 & layer2 are sublayers of the button layer.



            You could add a third layer that will serve as a background. Here is an example based on your code:



            class PrimaryButton: UIButton 

            let layer1 = CALayer(), layer2 = CALayer(), layer3 = CALayer()

            override func layoutSubviews()
            super.layoutSubviews()
            layer1.backgroundColor = UIColor.blue.cgColor
            layer1.cornerRadius = 48 / 2
            [layer1, layer2, layer3].forEach
            $0.masksToBounds = false
            $0.frame = layer.bounds
            layer.insertSublayer($0, at: 0)

            layer2.applySketchShadow(color: UIColor.red, alpha: 0.5, x: 0, y: 15, blur: 35, spread: -10)
            layer3.applySketchShadow(color: UIColor.blue, alpha: 0.5, x: 0, y: 10, blur: 21, spread: -9)




            Note that I put most of the code inside layoutSubviews because most of your methods use the actual bounds of the button.






            share|improve this answer















            It seems legit to me. layer1 & layer2 are sublayers of the button layer.



            You could add a third layer that will serve as a background. Here is an example based on your code:



            class PrimaryButton: UIButton 

            let layer1 = CALayer(), layer2 = CALayer(), layer3 = CALayer()

            override func layoutSubviews()
            super.layoutSubviews()
            layer1.backgroundColor = UIColor.blue.cgColor
            layer1.cornerRadius = 48 / 2
            [layer1, layer2, layer3].forEach
            $0.masksToBounds = false
            $0.frame = layer.bounds
            layer.insertSublayer($0, at: 0)

            layer2.applySketchShadow(color: UIColor.red, alpha: 0.5, x: 0, y: 15, blur: 35, spread: -10)
            layer3.applySketchShadow(color: UIColor.blue, alpha: 0.5, x: 0, y: 10, blur: 21, spread: -9)




            Note that I put most of the code inside layoutSubviews because most of your methods use the actual bounds of the button.







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Mar 21 at 21:16

























            answered Mar 21 at 17:49









            GaétanZGaétanZ

            1,95411226




            1,95411226












            • layer.masksToBounds = false is set 3 times? $0 or before forEach?

              – user317706
              Mar 21 at 21:07











            • @user317706 I edited the answer. You are right.

              – GaétanZ
              Mar 21 at 21:17

















            • layer.masksToBounds = false is set 3 times? $0 or before forEach?

              – user317706
              Mar 21 at 21:07











            • @user317706 I edited the answer. You are right.

              – GaétanZ
              Mar 21 at 21:17
















            layer.masksToBounds = false is set 3 times? $0 or before forEach?

            – user317706
            Mar 21 at 21:07





            layer.masksToBounds = false is set 3 times? $0 or before forEach?

            – user317706
            Mar 21 at 21:07













            @user317706 I edited the answer. You are right.

            – GaétanZ
            Mar 21 at 21:17





            @user317706 I edited the answer. You are right.

            – GaétanZ
            Mar 21 at 21:17













            1














            Change your insertions to:



             layer.insertSublayer(layer2, at: 1)
            layer.insertSublayer(layer3, at: 2)


            That should do it.






            share|improve this answer























            • It did. Why? What is so special about the layer at index 0?

              – user317706
              Mar 21 at 20:36












            • I don't know why this works because everything I'm reading on z-ordering says it shouldn't and your original code should be correct. I just had a feeling and tried it :)

              – cbiggin
              Mar 21 at 20:56






            • 1





              It is not working. Take a look a the blue background side by side. There are not the same. You just switched the blue with the red.

              – GaétanZ
              Mar 21 at 21:28











            • Also had some trouble when implementing in my real solution. Have been looking at too many shadows today. The idea of having a background layer and 2 shadow layers worked fine.

              – user317706
              Mar 21 at 21:45















            1














            Change your insertions to:



             layer.insertSublayer(layer2, at: 1)
            layer.insertSublayer(layer3, at: 2)


            That should do it.






            share|improve this answer























            • It did. Why? What is so special about the layer at index 0?

              – user317706
              Mar 21 at 20:36












            • I don't know why this works because everything I'm reading on z-ordering says it shouldn't and your original code should be correct. I just had a feeling and tried it :)

              – cbiggin
              Mar 21 at 20:56






            • 1





              It is not working. Take a look a the blue background side by side. There are not the same. You just switched the blue with the red.

              – GaétanZ
              Mar 21 at 21:28











            • Also had some trouble when implementing in my real solution. Have been looking at too many shadows today. The idea of having a background layer and 2 shadow layers worked fine.

              – user317706
              Mar 21 at 21:45













            1












            1








            1







            Change your insertions to:



             layer.insertSublayer(layer2, at: 1)
            layer.insertSublayer(layer3, at: 2)


            That should do it.






            share|improve this answer













            Change your insertions to:



             layer.insertSublayer(layer2, at: 1)
            layer.insertSublayer(layer3, at: 2)


            That should do it.







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Mar 21 at 17:53









            cbiggincbiggin

            1,2341815




            1,2341815












            • It did. Why? What is so special about the layer at index 0?

              – user317706
              Mar 21 at 20:36












            • I don't know why this works because everything I'm reading on z-ordering says it shouldn't and your original code should be correct. I just had a feeling and tried it :)

              – cbiggin
              Mar 21 at 20:56






            • 1





              It is not working. Take a look a the blue background side by side. There are not the same. You just switched the blue with the red.

              – GaétanZ
              Mar 21 at 21:28











            • Also had some trouble when implementing in my real solution. Have been looking at too many shadows today. The idea of having a background layer and 2 shadow layers worked fine.

              – user317706
              Mar 21 at 21:45

















            • It did. Why? What is so special about the layer at index 0?

              – user317706
              Mar 21 at 20:36












            • I don't know why this works because everything I'm reading on z-ordering says it shouldn't and your original code should be correct. I just had a feeling and tried it :)

              – cbiggin
              Mar 21 at 20:56






            • 1





              It is not working. Take a look a the blue background side by side. There are not the same. You just switched the blue with the red.

              – GaétanZ
              Mar 21 at 21:28











            • Also had some trouble when implementing in my real solution. Have been looking at too many shadows today. The idea of having a background layer and 2 shadow layers worked fine.

              – user317706
              Mar 21 at 21:45
















            It did. Why? What is so special about the layer at index 0?

            – user317706
            Mar 21 at 20:36






            It did. Why? What is so special about the layer at index 0?

            – user317706
            Mar 21 at 20:36














            I don't know why this works because everything I'm reading on z-ordering says it shouldn't and your original code should be correct. I just had a feeling and tried it :)

            – cbiggin
            Mar 21 at 20:56





            I don't know why this works because everything I'm reading on z-ordering says it shouldn't and your original code should be correct. I just had a feeling and tried it :)

            – cbiggin
            Mar 21 at 20:56




            1




            1





            It is not working. Take a look a the blue background side by side. There are not the same. You just switched the blue with the red.

            – GaétanZ
            Mar 21 at 21:28





            It is not working. Take a look a the blue background side by side. There are not the same. You just switched the blue with the red.

            – GaétanZ
            Mar 21 at 21:28













            Also had some trouble when implementing in my real solution. Have been looking at too many shadows today. The idea of having a background layer and 2 shadow layers worked fine.

            – user317706
            Mar 21 at 21:45





            Also had some trouble when implementing in my real solution. Have been looking at too many shadows today. The idea of having a background layer and 2 shadow layers worked fine.

            – user317706
            Mar 21 at 21:45











            0














            Another way is to add double buttons without change your class.



             let button = PrimaryButton()
            button.frame = CGRect(x: 150, y: 200, width: 200, height: 48)
            button.backgroundColor = UIColor.clear
            view.addSubview(button)
            self.view = view
            let button1 = PrimaryButton()
            button1.frame = CGRect(x: 0, y: 0, width: 200, height: 48)
            button.addSubview(button1)
            button1.layer.sublayers?.forEach$0.removeFromSuperlayer()





            share|improve this answer



























              0














              Another way is to add double buttons without change your class.



               let button = PrimaryButton()
              button.frame = CGRect(x: 150, y: 200, width: 200, height: 48)
              button.backgroundColor = UIColor.clear
              view.addSubview(button)
              self.view = view
              let button1 = PrimaryButton()
              button1.frame = CGRect(x: 0, y: 0, width: 200, height: 48)
              button.addSubview(button1)
              button1.layer.sublayers?.forEach$0.removeFromSuperlayer()





              share|improve this answer

























                0












                0








                0







                Another way is to add double buttons without change your class.



                 let button = PrimaryButton()
                button.frame = CGRect(x: 150, y: 200, width: 200, height: 48)
                button.backgroundColor = UIColor.clear
                view.addSubview(button)
                self.view = view
                let button1 = PrimaryButton()
                button1.frame = CGRect(x: 0, y: 0, width: 200, height: 48)
                button.addSubview(button1)
                button1.layer.sublayers?.forEach$0.removeFromSuperlayer()





                share|improve this answer













                Another way is to add double buttons without change your class.



                 let button = PrimaryButton()
                button.frame = CGRect(x: 150, y: 200, width: 200, height: 48)
                button.backgroundColor = UIColor.clear
                view.addSubview(button)
                self.view = view
                let button1 = PrimaryButton()
                button1.frame = CGRect(x: 0, y: 0, width: 200, height: 48)
                button.addSubview(button1)
                button1.layer.sublayers?.forEach$0.removeFromSuperlayer()






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Mar 21 at 18:14









                E.ComsE.Coms

                3,1492416




                3,1492416



























                    draft saved

                    draft discarded
















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid


                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.

                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55285700%2fmultiple-shadows-under-uiview-ios-swift%23new-answer', 'question_page');

                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

                    Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

                    Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript