Быстро 5 Просто вызывают эту функцию и передают Ваше представление
public func setViewSettingWithBgShade(view: UIView)
{
view.layer.cornerRadius = 8
view.layer.borderWidth = 1
view.layer.borderColor = AppTextFieldBorderColor.cgColor
//MARK:- Shade a view
view.layer.shadowOpacity = 0.5
view.layer.shadowOffset = CGSize(width: 1.0, height: 1.0)
view.layer.shadowRadius = 3.0
view.layer.shadowColor = UIColor.black.cgColor
view.layer.masksToBounds = false
}
Мы можем применить падающую тень следующим путем также
cell.view1.layer.masksToBounds = false
cell.view1.layer.shadowColor = UIColor.lightGray.cgColor
cell.view1.backgroundColor = UIColor.white
cell.view1.layer.shadowOffset = CGSize(width: 1.0, height: 1.0)
cell.view1.layer.shadowOpacity = 0.5
, Результат будет: http://prntscr.com/nhhv2s
Попробуйте это, это работает на меня.
extension UIView {
func dropShadow() {
layer.shadowColor = UIColor.black.cgColor
layer.shadowOffset = CGSize(width: 2, height: 3)
layer.masksToBounds = false
layer.shadowOpacity = 0.3
layer.shadowRadius = 3
//layer.shadowPath = UIBezierPath(rect: bounds).cgPath
layer.rasterizationScale = UIScreen.main.scale
layer.shouldRasterize = true
}}
Очень простое в использовании расширение для UIView, доступного для редактирования непосредственно от раскадровки. Быстро 4 +
@IBDesignable extension UIView {
@IBInspectable var shadowColor: UIColor?{
set {
guard let uiColor = newValue else { return }
layer.shadowColor = uiColor.cgColor
}
get{
guard let color = layer.shadowColor else { return nil }
return UIColor(cgColor: color)
}
}
@IBInspectable var shadowOpacity: Float{
set {
layer.shadowOpacity = newValue
}
get{
return layer.shadowOpacity
}
}
@IBInspectable var shadowOffset: CGSize{
set {
layer.shadowOffset = newValue
}
get{
return layer.shadowOffset
}
}
@IBInspectable var shadowRadius: CGFloat{
set {
layer.shadowRadius = newValue
}
get{
return layer.shadowRadius
}
}
}
Попробуйте это
func applyShadowOnView(_ view: UIView) {
view.layer.cornerRadius = 8
view.layer.shadowColor = UIColor.darkGray.cgColor
view.layer.shadowOpacity = 1
view.layer.shadowOffset = .zero
view.layer.shadowRadius = 5
}
Если Вам нужна округленная тень. Работы для быстрого 4.2
extension UIView {
func dropShadow() {
var shadowLayer: CAShapeLayer!
let cornerRadius: CGFloat = 16.0
let fillColor: UIColor = .white
if shadowLayer == nil {
shadowLayer = CAShapeLayer()
shadowLayer.path = UIBezierPath(roundedRect: bounds, cornerRadius: cornerRadius).cgPath
shadowLayer.fillColor = fillColor.cgColor
shadowLayer.shadowColor = UIColor.black.cgColor
shadowLayer.shadowPath = shadowLayer.path
shadowLayer.shadowOffset = CGSize(width: -2.0, height: 2.0)
shadowLayer.shadowOpacity = 0.8
shadowLayer.shadowRadius = 2
layer.insertSublayer(shadowLayer, at: 0)
}
}
}