Swift - How to apply the drop shadow only on UIButton Bottom Shadow but not for its image and its title...











up vote
0
down vote

favorite












I'm trying to add drop shadow to UIButton but I want to have only the shadow for UIButton bottom shadow only not for its image and title. I followed UIButton bottom shadow but it didn't work.
Basically, here is what I'm having right now:
enter image description here



And here is what I want to have:
enter image description here



This is my current code:



button.layer.borderWidth = 0.5
button.layer.borderColor = UIColor.gray.cgColor
button.layer.shadowColor = UIColor.black.cgColor
button.layer.shadowOffset = CGSize(width: 0, height: 2)
button.layer.shadowOpacity = 1.0
button.layer.shadowRadius = 0
button.layer.masksToBounds = false


Please help. Thanks in advance.










share|improve this question


















  • 1




    It looks like your button has transparent background, changing it's background to the white should do the trick.
    – Fahri Azimov
    Nov 5 at 3:43















up vote
0
down vote

favorite












I'm trying to add drop shadow to UIButton but I want to have only the shadow for UIButton bottom shadow only not for its image and title. I followed UIButton bottom shadow but it didn't work.
Basically, here is what I'm having right now:
enter image description here



And here is what I want to have:
enter image description here



This is my current code:



button.layer.borderWidth = 0.5
button.layer.borderColor = UIColor.gray.cgColor
button.layer.shadowColor = UIColor.black.cgColor
button.layer.shadowOffset = CGSize(width: 0, height: 2)
button.layer.shadowOpacity = 1.0
button.layer.shadowRadius = 0
button.layer.masksToBounds = false


Please help. Thanks in advance.










share|improve this question


















  • 1




    It looks like your button has transparent background, changing it's background to the white should do the trick.
    – Fahri Azimov
    Nov 5 at 3:43













up vote
0
down vote

favorite









up vote
0
down vote

favorite











I'm trying to add drop shadow to UIButton but I want to have only the shadow for UIButton bottom shadow only not for its image and title. I followed UIButton bottom shadow but it didn't work.
Basically, here is what I'm having right now:
enter image description here



And here is what I want to have:
enter image description here



This is my current code:



button.layer.borderWidth = 0.5
button.layer.borderColor = UIColor.gray.cgColor
button.layer.shadowColor = UIColor.black.cgColor
button.layer.shadowOffset = CGSize(width: 0, height: 2)
button.layer.shadowOpacity = 1.0
button.layer.shadowRadius = 0
button.layer.masksToBounds = false


Please help. Thanks in advance.










share|improve this question













I'm trying to add drop shadow to UIButton but I want to have only the shadow for UIButton bottom shadow only not for its image and title. I followed UIButton bottom shadow but it didn't work.
Basically, here is what I'm having right now:
enter image description here



And here is what I want to have:
enter image description here



This is my current code:



button.layer.borderWidth = 0.5
button.layer.borderColor = UIColor.gray.cgColor
button.layer.shadowColor = UIColor.black.cgColor
button.layer.shadowOffset = CGSize(width: 0, height: 2)
button.layer.shadowOpacity = 1.0
button.layer.shadowRadius = 0
button.layer.masksToBounds = false


Please help. Thanks in advance.







ios swift uibutton uikit






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 5 at 3:40









Va Visal

744816




744816








  • 1




    It looks like your button has transparent background, changing it's background to the white should do the trick.
    – Fahri Azimov
    Nov 5 at 3:43














  • 1




    It looks like your button has transparent background, changing it's background to the white should do the trick.
    – Fahri Azimov
    Nov 5 at 3:43








1




1




It looks like your button has transparent background, changing it's background to the white should do the trick.
– Fahri Azimov
Nov 5 at 3:43




It looks like your button has transparent background, changing it's background to the white should do the trick.
– Fahri Azimov
Nov 5 at 3:43












3 Answers
3






active

oldest

votes

















up vote
0
down vote













Hi dear best way to apply shadow for transparent button you just need to embed your button in view and apply shadow effect on that view.



Like I have done below:



yourButtonView.layer.borderWidth = 0.5

yourButtonView.layer.borderColor = UIColor.gray.cgColor

yourButtonView.layer.shadowColor = UIColor.black.cgColor

yourButtonView.layer.shadowOffset = CGSize(width: 0, height: 2)

yourButtonView.layer.shadowOpacity = 1.0

yourButtonView.layer.shadowRadius = 0

yourButtonView.layer.masksToBounds = false





share|improve this answer






























    up vote
    0
    down vote













    Please use the below extension for creating shadow



    extension UIView {

    func addshadow(top: Bool,
    left: Bool,
    bottom: Bool,
    right: Bool
    ) {

    let shadowRadius: CGFloat = 2.0
    self.layer.masksToBounds = false
    self.layer.shadowOffset = CGSize(width: 0.0, height: 0.0)
    self.layer.shadowRadius = shadowRadius
    self.layer.shadowOpacity = 0.3
    let path = UIBezierPath()
    var x: CGFloat = 0
    var y: CGFloat = 0
    var viewWidth = self.frame.width
    var viewHeight = self.frame.height
    if (!top) {
    y+=(shadowRadius+1)
    }
    if (!bottom) {
    viewHeight-=(shadowRadius+1)
    }
    if (!left) {
    x+=(shadowRadius+1)
    }
    if (!right) {
    viewWidth-=(shadowRadius+1)
    }
    path.move(to: CGPoint(x: x, y: y))
    path.addLine(to: CGPoint(x: x, y: viewHeight))
    path.addLine(to: CGPoint(x: viewWidth, y: viewHeight))
    path.addLine(to: CGPoint(x: viewWidth, y: y))
    path.close()
    self.layer.shadowPath = path.cgPath
    }

    }


    use -



        button.addshadow(top: false, left: false, bottom: true, right: false)





    share|improve this answer




























      up vote
      0
      down vote













      Check output in Attached image



      Checkout below lines of code



      btn.setImage(UIImage(named: "Unknown.jpg"), for: .normal)

      btn.imageView?.layer.shadowColor = UIColor.blue.cgColor

      btn.imageView?.layer.shadowOffset = CGSize(width: 1, height: 1)

      btn.imageView?.layer.shadowOpacity = 1.0

      btn.imageView?.layer.shadowRadius = 5

      btn.imageView?.layer.masksToBounds = false

      btn.setTitle(" hello", for: .normal)

      btn.titleLabel?.layer.shadowColor = UIColor.black.cgColor

      btn.titleLabel?.layer.shadowOffset = CGSize(width: 1, height: 1)

      btn.titleLabel?.layer.shadowOpacity = 1.0

      btn.titleLabel?.layer.shadowRadius = 3

      btn.titleLabel?.layer.masksToBounds = false

      btn.backgroundColor = UIColor.red





      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',
        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%2f53148010%2fswift-how-to-apply-the-drop-shadow-only-on-uibutton-bottom-shadow-but-not-for%23new-answer', 'question_page');
        }
        );

        Post as a guest
































        3 Answers
        3






        active

        oldest

        votes








        3 Answers
        3






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes








        up vote
        0
        down vote













        Hi dear best way to apply shadow for transparent button you just need to embed your button in view and apply shadow effect on that view.



        Like I have done below:



        yourButtonView.layer.borderWidth = 0.5

        yourButtonView.layer.borderColor = UIColor.gray.cgColor

        yourButtonView.layer.shadowColor = UIColor.black.cgColor

        yourButtonView.layer.shadowOffset = CGSize(width: 0, height: 2)

        yourButtonView.layer.shadowOpacity = 1.0

        yourButtonView.layer.shadowRadius = 0

        yourButtonView.layer.masksToBounds = false





        share|improve this answer



























          up vote
          0
          down vote













          Hi dear best way to apply shadow for transparent button you just need to embed your button in view and apply shadow effect on that view.



          Like I have done below:



          yourButtonView.layer.borderWidth = 0.5

          yourButtonView.layer.borderColor = UIColor.gray.cgColor

          yourButtonView.layer.shadowColor = UIColor.black.cgColor

          yourButtonView.layer.shadowOffset = CGSize(width: 0, height: 2)

          yourButtonView.layer.shadowOpacity = 1.0

          yourButtonView.layer.shadowRadius = 0

          yourButtonView.layer.masksToBounds = false





          share|improve this answer

























            up vote
            0
            down vote










            up vote
            0
            down vote









            Hi dear best way to apply shadow for transparent button you just need to embed your button in view and apply shadow effect on that view.



            Like I have done below:



            yourButtonView.layer.borderWidth = 0.5

            yourButtonView.layer.borderColor = UIColor.gray.cgColor

            yourButtonView.layer.shadowColor = UIColor.black.cgColor

            yourButtonView.layer.shadowOffset = CGSize(width: 0, height: 2)

            yourButtonView.layer.shadowOpacity = 1.0

            yourButtonView.layer.shadowRadius = 0

            yourButtonView.layer.masksToBounds = false





            share|improve this answer














            Hi dear best way to apply shadow for transparent button you just need to embed your button in view and apply shadow effect on that view.



            Like I have done below:



            yourButtonView.layer.borderWidth = 0.5

            yourButtonView.layer.borderColor = UIColor.gray.cgColor

            yourButtonView.layer.shadowColor = UIColor.black.cgColor

            yourButtonView.layer.shadowOffset = CGSize(width: 0, height: 2)

            yourButtonView.layer.shadowOpacity = 1.0

            yourButtonView.layer.shadowRadius = 0

            yourButtonView.layer.masksToBounds = false






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Nov 5 at 4:56









            D V

            29819




            29819










            answered Nov 5 at 3:57









            Ahemadabbas Vagh

            395




            395
























                up vote
                0
                down vote













                Please use the below extension for creating shadow



                extension UIView {

                func addshadow(top: Bool,
                left: Bool,
                bottom: Bool,
                right: Bool
                ) {

                let shadowRadius: CGFloat = 2.0
                self.layer.masksToBounds = false
                self.layer.shadowOffset = CGSize(width: 0.0, height: 0.0)
                self.layer.shadowRadius = shadowRadius
                self.layer.shadowOpacity = 0.3
                let path = UIBezierPath()
                var x: CGFloat = 0
                var y: CGFloat = 0
                var viewWidth = self.frame.width
                var viewHeight = self.frame.height
                if (!top) {
                y+=(shadowRadius+1)
                }
                if (!bottom) {
                viewHeight-=(shadowRadius+1)
                }
                if (!left) {
                x+=(shadowRadius+1)
                }
                if (!right) {
                viewWidth-=(shadowRadius+1)
                }
                path.move(to: CGPoint(x: x, y: y))
                path.addLine(to: CGPoint(x: x, y: viewHeight))
                path.addLine(to: CGPoint(x: viewWidth, y: viewHeight))
                path.addLine(to: CGPoint(x: viewWidth, y: y))
                path.close()
                self.layer.shadowPath = path.cgPath
                }

                }


                use -



                    button.addshadow(top: false, left: false, bottom: true, right: false)





                share|improve this answer

























                  up vote
                  0
                  down vote













                  Please use the below extension for creating shadow



                  extension UIView {

                  func addshadow(top: Bool,
                  left: Bool,
                  bottom: Bool,
                  right: Bool
                  ) {

                  let shadowRadius: CGFloat = 2.0
                  self.layer.masksToBounds = false
                  self.layer.shadowOffset = CGSize(width: 0.0, height: 0.0)
                  self.layer.shadowRadius = shadowRadius
                  self.layer.shadowOpacity = 0.3
                  let path = UIBezierPath()
                  var x: CGFloat = 0
                  var y: CGFloat = 0
                  var viewWidth = self.frame.width
                  var viewHeight = self.frame.height
                  if (!top) {
                  y+=(shadowRadius+1)
                  }
                  if (!bottom) {
                  viewHeight-=(shadowRadius+1)
                  }
                  if (!left) {
                  x+=(shadowRadius+1)
                  }
                  if (!right) {
                  viewWidth-=(shadowRadius+1)
                  }
                  path.move(to: CGPoint(x: x, y: y))
                  path.addLine(to: CGPoint(x: x, y: viewHeight))
                  path.addLine(to: CGPoint(x: viewWidth, y: viewHeight))
                  path.addLine(to: CGPoint(x: viewWidth, y: y))
                  path.close()
                  self.layer.shadowPath = path.cgPath
                  }

                  }


                  use -



                      button.addshadow(top: false, left: false, bottom: true, right: false)





                  share|improve this answer























                    up vote
                    0
                    down vote










                    up vote
                    0
                    down vote









                    Please use the below extension for creating shadow



                    extension UIView {

                    func addshadow(top: Bool,
                    left: Bool,
                    bottom: Bool,
                    right: Bool
                    ) {

                    let shadowRadius: CGFloat = 2.0
                    self.layer.masksToBounds = false
                    self.layer.shadowOffset = CGSize(width: 0.0, height: 0.0)
                    self.layer.shadowRadius = shadowRadius
                    self.layer.shadowOpacity = 0.3
                    let path = UIBezierPath()
                    var x: CGFloat = 0
                    var y: CGFloat = 0
                    var viewWidth = self.frame.width
                    var viewHeight = self.frame.height
                    if (!top) {
                    y+=(shadowRadius+1)
                    }
                    if (!bottom) {
                    viewHeight-=(shadowRadius+1)
                    }
                    if (!left) {
                    x+=(shadowRadius+1)
                    }
                    if (!right) {
                    viewWidth-=(shadowRadius+1)
                    }
                    path.move(to: CGPoint(x: x, y: y))
                    path.addLine(to: CGPoint(x: x, y: viewHeight))
                    path.addLine(to: CGPoint(x: viewWidth, y: viewHeight))
                    path.addLine(to: CGPoint(x: viewWidth, y: y))
                    path.close()
                    self.layer.shadowPath = path.cgPath
                    }

                    }


                    use -



                        button.addshadow(top: false, left: false, bottom: true, right: false)





                    share|improve this answer












                    Please use the below extension for creating shadow



                    extension UIView {

                    func addshadow(top: Bool,
                    left: Bool,
                    bottom: Bool,
                    right: Bool
                    ) {

                    let shadowRadius: CGFloat = 2.0
                    self.layer.masksToBounds = false
                    self.layer.shadowOffset = CGSize(width: 0.0, height: 0.0)
                    self.layer.shadowRadius = shadowRadius
                    self.layer.shadowOpacity = 0.3
                    let path = UIBezierPath()
                    var x: CGFloat = 0
                    var y: CGFloat = 0
                    var viewWidth = self.frame.width
                    var viewHeight = self.frame.height
                    if (!top) {
                    y+=(shadowRadius+1)
                    }
                    if (!bottom) {
                    viewHeight-=(shadowRadius+1)
                    }
                    if (!left) {
                    x+=(shadowRadius+1)
                    }
                    if (!right) {
                    viewWidth-=(shadowRadius+1)
                    }
                    path.move(to: CGPoint(x: x, y: y))
                    path.addLine(to: CGPoint(x: x, y: viewHeight))
                    path.addLine(to: CGPoint(x: viewWidth, y: viewHeight))
                    path.addLine(to: CGPoint(x: viewWidth, y: y))
                    path.close()
                    self.layer.shadowPath = path.cgPath
                    }

                    }


                    use -



                        button.addshadow(top: false, left: false, bottom: true, right: false)






                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Nov 5 at 5:04









                    Abhinav Jha

                    12410




                    12410






















                        up vote
                        0
                        down vote













                        Check output in Attached image



                        Checkout below lines of code



                        btn.setImage(UIImage(named: "Unknown.jpg"), for: .normal)

                        btn.imageView?.layer.shadowColor = UIColor.blue.cgColor

                        btn.imageView?.layer.shadowOffset = CGSize(width: 1, height: 1)

                        btn.imageView?.layer.shadowOpacity = 1.0

                        btn.imageView?.layer.shadowRadius = 5

                        btn.imageView?.layer.masksToBounds = false

                        btn.setTitle(" hello", for: .normal)

                        btn.titleLabel?.layer.shadowColor = UIColor.black.cgColor

                        btn.titleLabel?.layer.shadowOffset = CGSize(width: 1, height: 1)

                        btn.titleLabel?.layer.shadowOpacity = 1.0

                        btn.titleLabel?.layer.shadowRadius = 3

                        btn.titleLabel?.layer.masksToBounds = false

                        btn.backgroundColor = UIColor.red





                        share|improve this answer



























                          up vote
                          0
                          down vote













                          Check output in Attached image



                          Checkout below lines of code



                          btn.setImage(UIImage(named: "Unknown.jpg"), for: .normal)

                          btn.imageView?.layer.shadowColor = UIColor.blue.cgColor

                          btn.imageView?.layer.shadowOffset = CGSize(width: 1, height: 1)

                          btn.imageView?.layer.shadowOpacity = 1.0

                          btn.imageView?.layer.shadowRadius = 5

                          btn.imageView?.layer.masksToBounds = false

                          btn.setTitle(" hello", for: .normal)

                          btn.titleLabel?.layer.shadowColor = UIColor.black.cgColor

                          btn.titleLabel?.layer.shadowOffset = CGSize(width: 1, height: 1)

                          btn.titleLabel?.layer.shadowOpacity = 1.0

                          btn.titleLabel?.layer.shadowRadius = 3

                          btn.titleLabel?.layer.masksToBounds = false

                          btn.backgroundColor = UIColor.red





                          share|improve this answer

























                            up vote
                            0
                            down vote










                            up vote
                            0
                            down vote









                            Check output in Attached image



                            Checkout below lines of code



                            btn.setImage(UIImage(named: "Unknown.jpg"), for: .normal)

                            btn.imageView?.layer.shadowColor = UIColor.blue.cgColor

                            btn.imageView?.layer.shadowOffset = CGSize(width: 1, height: 1)

                            btn.imageView?.layer.shadowOpacity = 1.0

                            btn.imageView?.layer.shadowRadius = 5

                            btn.imageView?.layer.masksToBounds = false

                            btn.setTitle(" hello", for: .normal)

                            btn.titleLabel?.layer.shadowColor = UIColor.black.cgColor

                            btn.titleLabel?.layer.shadowOffset = CGSize(width: 1, height: 1)

                            btn.titleLabel?.layer.shadowOpacity = 1.0

                            btn.titleLabel?.layer.shadowRadius = 3

                            btn.titleLabel?.layer.masksToBounds = false

                            btn.backgroundColor = UIColor.red





                            share|improve this answer














                            Check output in Attached image



                            Checkout below lines of code



                            btn.setImage(UIImage(named: "Unknown.jpg"), for: .normal)

                            btn.imageView?.layer.shadowColor = UIColor.blue.cgColor

                            btn.imageView?.layer.shadowOffset = CGSize(width: 1, height: 1)

                            btn.imageView?.layer.shadowOpacity = 1.0

                            btn.imageView?.layer.shadowRadius = 5

                            btn.imageView?.layer.masksToBounds = false

                            btn.setTitle(" hello", for: .normal)

                            btn.titleLabel?.layer.shadowColor = UIColor.black.cgColor

                            btn.titleLabel?.layer.shadowOffset = CGSize(width: 1, height: 1)

                            btn.titleLabel?.layer.shadowOpacity = 1.0

                            btn.titleLabel?.layer.shadowRadius = 3

                            btn.titleLabel?.layer.masksToBounds = false

                            btn.backgroundColor = UIColor.red






                            share|improve this answer














                            share|improve this answer



                            share|improve this answer








                            edited Nov 5 at 5:58

























                            answered Nov 5 at 5:40









                            Sakshi

                            762819




                            762819






























                                 

                                draft saved


                                draft discarded



















































                                 


                                draft saved


                                draft discarded














                                StackExchange.ready(
                                function () {
                                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53148010%2fswift-how-to-apply-the-drop-shadow-only-on-uibutton-bottom-shadow-but-not-for%23new-answer', 'question_page');
                                }
                                );

                                Post as a guest




















































































                                這個網誌中的熱門文章

                                Xamarin.form Move up view when keyboard appear

                                Post-Redirect-Get with Spring WebFlux and Thymeleaf

                                Anylogic : not able to use stopDelay()