我需要创建带有阴影的UIView,但是我只需要在右,左,下边缘阴影->上边缘没有阴影。有可能这样做吗?我尝试了不同的偏移量,但是没有达到目标。
答案 0 :(得分:0)
像这样吗?
let yourView = UIView()
yourView.frame = CGRect(x: 50, y: 50, width: 100, height: 100)
yourView.backgroundColor = UIColor.blue // NEEDS A COLOR TO SHOW SHADOW
yourView.layer.shadowColor = UIColor.black.cgColor
yourView.layer.shadowOpacity = 1
yourView.layer.shadowOffset = CGSize.zero
yourView.layer.shadowRadius = 10
self.view.addSubview(yourView)
答案 1 :(得分:0)
我个人认为,克服后一个问题的最佳方法是创建UIView的@IBDesignable扩展并声明@IBInspectable属性,如下所述
public extension UIView {
// MARK: - Inspectables
/**
Defines the color of the shadow behind the view (defaults to nil).
*/
@IBInspectable public var shadowColor: UIColor? {
set {
if let color = newValue {
self.layer.shadowColor = color.cgColor
} else {
self.layer.shadowColor = nil
}
} get {
if let color = layer.shadowColor {
return UIColor(cgColor: color)
}
return nil
}
}
/**
Defines the radius of the shadow behind the view.
*/
@IBInspectable public var shadowRadius: CGFloat {
set(newValue) {
self.layer.shadowRadius = newValue
} get {
return self.layer.shadowRadius
}
}
/**
Defines the opacity of the shadow behind the view.
*/
@IBInspectable public var shadowOpacity: Float {
set(newValue) {
self.layer.shadowOpacity = newValue
} get {
return self.layer.shadowOpacity
}
}
/**
Defines the offset of the shadow behind the view.
*/
@IBInspectable public var shadowOffset: CGSize {
set(newValue) {
self.layer.shadowOffset = newValue
} get {
return self.layer.shadowOffset
}
}
}
然后,您将能够在故事板上找到每个UIView(或从UIView继承的类)的后一个参数。
但是,如果您正在以编程方式进行工作,则必须尝试使用shadowOffset
属性,直到获得所需的视觉效果为止。