【问题标题】:Adding shadow to top of UIView在 UIView 顶部添加阴影
【发布时间】:2014-07-30 11:16:58
【问题描述】:

如何在我的UIView 顶部添加阴影我已经尝试了以下但没有运气...

childView.layer.cornerRadius = 5;
childView.layer.masksToBounds = YES;
childView.layer.shadowOffset = CGSizeMake(-15, 20);
childView.layer.shadowRadius = 5;
childView.layer.shadowOpacity = 0.5;

【问题讨论】:

  • 当您说“顶部”时,您的意思是“在前面”还是“在上面”?如果你的意思是上面的,CGSizeMake 上的y 值应该是负数。

标签: ios objective-c uiview


【解决方案1】:

答案在UIBezierPath

你可以在那里找到一篇非常有趣的文章: https://www.hackingwithswift.com/articles/155/advanced-uiview-shadow-effects-using-shadowpath

layer.shadowOffset = CGSize(width: 0, height: -2)
layer.shadowRadius = 5
layer.shadowPath = UIBezierPath(rect: CGRect(x: -1,
                                             y: -1,
                                             width: self.bounds.width,
                                             height: self.layer.shadowRadius)).cgPath

【讨论】:

    【解决方案2】:

    1.Step:首先创建一个uiview的outlet

    2.步骤:

        buttonBackgroundView.layer.shadowOpacity = 0.7
        buttonBackgroundView.layer.shadowOffset = CGSize(width: 3, height: 3)
        buttonBackgroundView.layer.shadowRadius = 15.0
        buttonBackgroundView.layer.shadowColor = UIColor.black.cgColor
    

    【讨论】:

      【解决方案3】:

      Swift 3 扩展:

      这包括我正在开发的应用程序的默认值,但您可以更改它们以匹配您希望在应用程序中使用的样式。

      enum VerticalLocation: String {
          case bottom
          case top  
      }
      
      extension UIView {
          func addShadow(location: VerticalLocation, color: UIColor = .black, opacity: Float = 0.5, radius: CGFloat = 5.0) {
              switch location {
              case .bottom:
                   addShadow(offset: CGSize(width: 0, height: 10), color: color, opacity: opacity, radius: radius)
              case .top:
                  addShadow(offset: CGSize(width: 0, height: -10), color: color, opacity: opacity, radius: radius)
              }
          }
      
          func addShadow(offset: CGSize, color: UIColor = .black, opacity: Float = 0.5, radius: CGFloat = 5.0) {
              self.layer.masksToBounds = false
              self.layer.shadowColor = color.cgColor
              self.layer.shadowOffset = offset
              self.layer.shadowOpacity = opacity
              self.layer.shadowRadius = radius
          }
      }
      

      【讨论】:

      • 这似乎给所有子视图添加了阴影
      【解决方案4】:

      设置您的masksToBounds = NO。您看不到阴影的原因是当 maskToBounds 为 YES 时,它完全隐藏在您的视图后面。

      如果您的按钮是圆形的,您可以改为调整视图的 imageEdgeInset 值。即:UIEdgeInsetsMake(5, 0, 0, 10);

      【讨论】:

        【解决方案5】:

        您需要将子视图的masksToBounds 属性设置为NO 以使阴影可见。

        childView.layer.masksToBounds = NO;
        

        【讨论】:

        【解决方案6】:

        尝试将 maskToBounds 设置为 NO。根据此链接Whats the best way to add a drop shadow to my UIView 确定 YES 将剪切超出视图边界的图层。

        【讨论】:

          猜你喜欢
          • 2016-10-06
          • 1970-01-01
          • 1970-01-01
          • 2015-10-23
          • 1970-01-01
          • 2018-07-19
          • 1970-01-01
          • 2018-11-06
          相关资源
          最近更新 更多