【问题标题】:IOS: How to make a shadow for UIView on 4 side (top,right,bottom and left)IOS:如何在 4 侧(顶部、右侧、底部和左侧)为 UIView 制作阴影
【发布时间】:2016-07-10 10:36:13
【问题描述】:

我正在使用下面的代码为我的ImageView 制作阴影

UIBezierPath *shadowPath = [UIBezierPath bezierPathWithRect:self.avatarImageView.bounds];
self.avatarImageView.layer.masksToBounds = NO;
self.avatarImageView.layer.shadowColor = [UIColor blackColor].CGColor;
self.avatarImageView.layer.shadowOffset = CGSizeMake(5.0f, 5.0f);
self.avatarImageView.layer.shadowOpacity = 0.8f;
self.avatarImageView.layer.shadowPath = shadowPath.CGPath;

它会像这张图片一样在右侧和底部投下阴影。

现在我想让我的ImageView 在顶部和左侧也有阴影。 我应该在代码中更改什么? 是否可以仅通过代码中的配置使视图在顶部、右侧、底部、左侧包含阴影,或者我需要为阴影创建其他布局视图?任何帮助将不胜感激。

这是我想要实现的目标

更新
感谢@Dipen Panchasara 提供了一个简单的解决方案。关注@Dipen Panchasara(阴影颜色为黑色)我会有这样的阴影图像

【问题讨论】:

    标签: ios uiview


    【解决方案1】:

    像这样:

    float shadowSize = 10.0f;
    UIBezierPath *shadowPath = [UIBezierPath bezierPathWithRect:CGRectMake(self.avatarImageView.frame.origin.x - shadowSize / 2,
                                                                           self.avatarImageView.frame.origin.y - shadowSize / 2,
                                                                           self.avatarImageView.frame.size.width + shadowSize,
                                                                           self.avatarImageView.frame.size.height + shadowSize)];
    self.avatarImageView.layer.masksToBounds = NO;
    self.avatarImageView.layer.shadowColor = [UIColor blackColor].CGColor;
    self.avatarImageView.layer.shadowOffset = CGSizeMake(0.0f, 0.0f);
    self.avatarImageView.layer.shadowOpacity = 0.8f;
    self.avatarImageView.layer.shadowPath = shadowPath.CGPath;
    

    Swift 3 版本:

        let shadowSize : CGFloat = 5.0
        let shadowPath = UIBezierPath(rect: CGRect(x: -shadowSize / 2,
                                                   y: -shadowSize / 2,
                                                   width: self.avatarImageView.frame.size.width + shadowSize,
                                                   height: self.avatarImageView.frame.size.height + shadowSize))
        self.avatarImageView.layer.masksToBounds = false
        self.avatarImageView.layer.shadowColor = UIColor.black.cgColor
        self.avatarImageView.layer.shadowOffset = CGSize(width: 0.0, height: 0.0)
        self.avatarImageView.layer.shadowOpacity = 0.5
        self.avatarImageView.layer.shadowPath = shadowPath.cgPath
    

    【讨论】:

    • 这将为 4 面提供阴影(顶部:5,左侧:5,底部:10,右侧:10)。工作完美,谢谢
    • @PhanVanLinh,给出的第一个示例应该产生:(上:5,左:5,下:5,右:5),第二个应该产生(上:2.5,左: 2.5,底部:2.5,右侧:2.5)
    • 也应该在viewDidLayoutSubviews中调用stackoverflow.com/a/56459563/1522584
    【解决方案2】:

    只有以下代码才能满足您的要求,您无需为影子路径创建UIBezierPath

    // *** Set masks bounds to NO to display shadow visible ***
    self.avatarImageView.layer.masksToBounds = NO;
    // *** Set light gray color as shown in sample ***
    self.avatarImageView.layer.shadowColor = [UIColor lightGrayColor].CGColor;
    // *** *** Use following to add Shadow top, left ***
    self.avatarImageView.layer.shadowOffset = CGSizeMake(-5.0f, -5.0f);
    
    // *** Use following to add Shadow bottom, right ***
    //self.avatarImageView.layer.shadowOffset = CGSizeMake(5.0f, 5.0f);
    
    // *** Use following to add Shadow top, left, bottom, right ***
    // avatarImageView.layer.shadowOffset = CGSizeZero;
    // avatarImageView.layer.shadowRadius = 5.0f;
    
    // *** Set shadowOpacity to full (1) ***
    self.avatarImageView.layer.shadowOpacity = 1.0f;
    

    【讨论】:

    • 感谢您的回答,但请按照您的代码进行操作,我将在左上角看到阴影。但我想要的是我想在右上角有一个阴影。也许我解释得不是很好。非常感谢您的时间。我不反对它:(
    • 我确定self.avatarImageView.layer.shadowOffset = CGSizeZero; 不起作用。它不显示阴影,它已经过测试;)
    • @Dipen Panchasara,我相信 OP 想要右上角和左下角的阴影。您的解决方案只允许阴影出现在一个角落。
    • @PhanVănLinh 要在所有四个侧面显示阴影,您需要添加 shadowRadius。我已经更新了我的答案。
    • @GuntisTreulands 我添加了左上右下、左上和右下的解决方案。
    【解决方案3】:

    CGRectInset(self.avatarImageView.bounds, -10.0, -10.0)

    【讨论】:

      【解决方案4】:

      swift 3 的代码更少:

          view.layer.shadowColor = UIColor.black.cgColor
          view.layer.shadowOpacity = 0.7
          view.layer.shadowOffset = CGSize.zero
          view.layer.shadowRadius = 4
          view.layer.shadowPath = UIBezierPath(rect: planView.bounds).cgPath
      

      【讨论】:

        【解决方案5】:

        对于 UIView 和添加阴影,记得给 UIView 设置背景颜色。

        如果背景颜色为clearColor,则不出现阴影。

        【讨论】:

          【解决方案6】:

          //如果你以前试过这个,你就知道会发生什么。角落会变圆,但阴影会消失。如果将masksToBounds 设置为false,则会出现阴影,但不会圆角。 //获取具有圆角半径的阴影 为具有清晰颜色的容器视图添加超级视图,并为超级视图应用阴影,为容器视图应用圆角半径。试试看。

             //view to apply shadow and corner radius
              containerView.layer.cornerRadius = 3
              containerView.clipsToBounds = true
              //superview of container View for to apply shadow 
              shadowView.layer.shadowOpacity = 0.1
              shadowView.layer.shadowRadius = 2.0
              shadowView.layer.masksToBounds = false
              shadowView.layer.shadowOffset = CGSize.zero
          
              shadowView.layer.shadowColor = UIColor.Black.cgColor
          
              shadowView.layer.shadowPath = UIBezierPath(roundedRect:containerView.bounds, cornerRadius: containerView.layer.cornerRadius).cgPath
              shadowView.layer.shouldRasterize = true
          

          【讨论】:

            【解决方案7】:
            **in swift 4**
            
              yourView.clipsToBounds = true
                yourView.layer.cornerRadius = 20
                yourView.layer.shadowPath = UIBezierPath(roundedRect: self.yourView.bounds,
                                 cornerRadius: self.DeletConversation.layer.cornerRadius).cgPath
                yourView.layer.shadowColor = UIColor(hexString: "color")?.cgColor
                DeletConversation.layer.shadowOpacity = 1
                DeletConversation.layer.shadowOffset = CGSize(width: 0, height: 1.0)
                DeletConversation.layer.shadowRadius = 1
                DeletConversation.layer.masksToBounds = false
            

            【讨论】:

              【解决方案8】:

              在同一个视图上圆角阴影的最佳解决方案,无需做clipsToBounds或maskToBounds

              func addShadow(cornerRadius: CGFloat, maskedCorners: CACornerMask, color: UIColor, offset: CGSize, opacity: Float, shadowRadius: CGFloat) {
                      self.layer.cornerRadius = cornerRadius
                      self.layer.maskedCorners = maskedCorners
                      self.layer.shadowColor = color.cgColor
                      self.layer.shadowOffset = offset
                      self.layer.shadowOpacity = opacity
                      self.layer.shadowRadius = shadowRadius
                  }
              

              【讨论】:

                【解决方案9】:

                如果您仍然没有得到正确的阴影,问题可能出在您添加代码的位置。 当你使用 UIBezierPath 时,你应该在 viewDidLayoutSubviews 中调用它。如果你调用 ViewDidLoad,你可能会得到错误的结果,因为视图布局过程可能没有完成。

                override func viewDidLayoutSubviews() {
                    super.viewDidLayoutSubviews()
                
                    //Shadow code here
                }
                

                【讨论】:

                  【解决方案10】:

                  不使用 UIBezierPath,CGSize.zero 是这里的关键

                  yourView.layer.masksToBounds = false
                  yourView?.layer.shadowColor = UIColor.red.cgColor
                  yourView?.layer.shadowOffset =  CGSize.zero
                  yourView?.layer.shadowOpacity = 0.5
                  yourView?.layer.shadowRadius = 4
                  

                  【讨论】:

                    【解决方案11】:

                    这里有一个非常详细的解释:https://www.hackingwithswift.com/example-code/uikit/how-to-add-a-shadow-to-a-uiview

                    如果有人在集合视图中没有顶部阴影,那么这可能会有所帮助:

                    我知道这对某些人来说可能很明显,但是如果你有一个带有标题和单元格的 CollectionView,请确保标题和单元格之间有空间,否则,单元格的顶部阴影会被标题。

                    要添加空间,只需使用 insetsForSectionAt 部分。

                    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
                        return UIEdgeInsets(top: 5, left: 0, bottom: 5, right: 0)
                    
                    }
                    

                    【讨论】:

                      猜你喜欢
                      • 2011-06-22
                      • 2011-11-24
                      • 1970-01-01
                      • 2014-06-02
                      • 1970-01-01
                      • 1970-01-01
                      • 2021-06-14
                      • 1970-01-01
                      • 1970-01-01
                      相关资源
                      最近更新 更多