如何在我的UIView顶部添加阴影我尝试过以下但没有运气……
childView.layer.cornerRadius = 5; childView.layer.masksToBounds = YES; childView.layer.shadowOffset = CGSizeMake(-15,20); childView.layer.shadowRadius = 5; childView.layer.shadowOpacity = 0.5;
解决方法
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),radius: radius) } } func addShadow(offset: CGSize,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 } }