[英]iOS shadow view in interface builder
I'm trying to add shadows to some views via interface builder. 我正在尝试通过界面生成器向某些视图添加阴影。 I can't seem to get shadows to work on my views.
我似乎无法在我的观点上留下阴影。 All the resources I look at point to this same code so I'm not sure what I'm doing wrong.
我看到的所有资源都指向同一代码,所以我不确定自己在做什么错。
Interface Builder 界面生成器
Interface Builder Extension code Interface Builder扩展代码
import Foundation
import UIKit
extension UIView {
//cut irrelevant code for SO Question
@IBInspectable
var masksToBounds: Bool {
get {
return layer.masksToBounds
}
set {
layer.masksToBounds = newValue
}
}
// Shadow handling
@IBInspectable
var shadowColor: UIColor? {
get {
if let color = layer.shadowColor {
return UIColor(cgColor: color)
}
return nil
}
set {
if let color = newValue {
layer.shadowColor = color.cgColor
} else {
layer.shadowColor = nil
}
}
}
@IBInspectable
var shadowOpacity: Float {
get {
return layer.opacity
}
set {
layer.opacity = newValue
}
}
@IBInspectable
var shadowRadius: CGFloat {
get {
return layer.shadowRadius
}
set {
layer.shadowRadius = newValue
}
}
@IBInspectable
var shadowOffset: CGSize {
get {
return layer.shadowOffset
}
set {
layer.shadowOffset = newValue
}
}
}
Views layout 视图布局
This is the result 这是结果
I guess it's your custom search bar to which you're adding shadow. 我想这是您要添加阴影的自定义搜索栏。 The reason its not visible I guess is because your both view in controller and storyboard of same size.
我猜它不可见的原因是因为您在控制器和情节提要中的视图均相同。 Try to make the storyboard view have a container view in it with some padding along to the storyboard view and add that shadow to that container view.
尝试使情节提要视图具有一个容器视图,并在情节提要视图中添加一些填充,然后将阴影添加到该容器视图。 This might show some results.
这可能会显示一些结果。
Your shadowOpacity
property is accessing the wrong layer property. 您的
shadowOpacity
属性正在访问错误的layer属性。 It's accessing layer.opacity
when it should be layer.shadowOpacity
. 它的访问
layer.opacity
当它应该是layer.shadowOpacity
。 Also, your shadowRadius
is way too large and should probably be around 5 (depending on what you're going for, of course), not 500. 另外,您的
shadowRadius
太大了,应该大约为5(当然,这取决于您要做什么),而不是500。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.