问题描述
使用用户定义的运行时属性"时,很难显示阴影.
I'm having a hard time getting a drop shadow to show when I am using "User Defined Runtime Attributes".
如下所示,如果我使用代码,这似乎完全可以正常工作.
It seems to work completely fine if I use code, as follows.
func formatView(view: UIView, cornerRadius: Bool) {
if (cornerRadius) {view.layer.cornerRadius = 12 }
view.layer.shadowColor = UIColor.black.cgColor
view.layer.shadowOffset = CGSize.zero
view.layer.shadowRadius = 3
view.layer.shadowOpacity = 0.3
}
但是当我尝试使用用户定义的运行时属性时,它不再显示.这些是我目前正在使用的.
But when I try it with User Defined Runtime Attributes it doesn't show anymore. These are the ones I'm currently using.
唯一奇怪的是,如果我删除了 layer.shadowColor
属性,那么它似乎又可以工作了.但是我再也无法控制颜色了.它似乎默认为黑色,但是如果我决定选择灰色,则无法更改它.
The only thing that is weird is if I remove the layer.shadowColor
attribute, then it seems to work again. But I can no longer control the color. It seems to default to black, but if I ever decide to choose a grey color instead, I wouldn't be able to change it.
这是因为Color属性是UIColor,shadowColor需要CGColor吗?
Is this because the Color Attribute is a UIColor and shadowColor expects a CGColor?
推荐答案
确实如您所说,因为用户定义的运行时属性"面板中的"颜色" 类型创建了一个" UIColor" ,但 layer.borderColor
保留 cgColor
类型.
It is indeed as you stated because the Color
type in the User Defined Runtime Attributes panel creates a UIColor
, but layer.borderColor
holds a cgColor
type.
您可以通过创建一个类别来解决此问题,该类别允许通过Interface Builder设置代理颜色:
You could solve this by creating a category that allows a proxy color to be set through Interface Builder:
extension CALayer {
var borderUIColor: UIColor {
set {
self.borderColor = newValue.cgColor
}
get {
return UIColor(cgColor: self.borderColor!)
}
}
}
但是,更好的方法是使用 IBDesignable 代替用户定义的运行时属性.
But a much nicer way is to use IBDesignable instead of User Defined Runtime Attributes, it is more clear.
您可以通过在项目中添加一个名为UIViewExtentions.swift的新swift文件(或将其粘贴到任何文件中)来实现此目的:
You do this by adding a new swift file named UIViewExtentions.swift in your project (or just paste this in any file) :
import UIKit
@IBDesignable extension UIView {
@IBInspectable var borderColor:UIColor? {
set {
layer.borderColor = newValue!.cgColor
}
get {
if let color = layer.borderColor {
return UIColor(cgColor:color)
}
else {
return nil
}
}
}
@IBInspectable var borderWidth:CGFloat {
set {
layer.borderWidth = newValue
}
get {
return layer.borderWidth
}
}
@IBInspectable var cornerRadius:CGFloat {
set {
layer.cornerRadius = newValue
clipsToBounds = newValue > 0
}
get {
return layer.cornerRadius
}
}
}
然后将在Interface Builder中为实用工具面板> Attributes Inspector中的每个按钮,imageView,标签等提供可用的
Then this will be available in Interface Builder for every button, imageView, label, etc. in the Utilities Panel > Attributes Inspector:
现在,如果您在属性"检查器中设置值并回顾用户定义的运行时属性",您会看到它们会自动为您淘汰!
Now if you set you values in the Attributes Inspector and look back at the User Defined Runtime Attributes, you'll see they are automatically filed out for you!
有关更多信息,请参见: http://nshipster.com/ibinspectable-ibdesignable/
For more, see: http://nshipster.com/ibinspectable-ibdesignable/
这篇关于使用用户定义的运行时属性的UIView Shadow的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!