我创建了IBDesignable
和IBInspectable
自定义类,为视图提供了阴影和角半径
但是当我将Designable
类分配给view
时,我的设计构建失败
这是我的代码
import Foundation
import UIKit
@IBDesignable
class DesignableView: UIView {
}
@IBDesignable
class DesignableButton: UIButton {
}
@IBDesignable
class DesignableLabel: UILabel {
}
@IBDesignable
class DesignableTableView: UITableView {
}
extension UIView {
@IBInspectable
var cornerRadius: CGFloat {
get {
return layer.cornerRadius
}
set {
layer.cornerRadius = newValue
}
}
@IBInspectable
var borderWidth: CGFloat {
get {
return layer.borderWidth
}
set {
layer.borderWidth = newValue
}
}
@IBInspectable
var borderColor: UIColor? {
get {
if let color = layer.borderColor {
return UIColor(cgColor: color)
}
return nil
}
set {
if let color = newValue {
layer.borderColor = color.cgColor
} else {
layer.borderColor = nil
}
}
}
@IBInspectable
var shadowRadius: CGFloat {
get {
return layer.shadowRadius
}
set {
layer.shadowRadius = newValue
}
}
@IBInspectable
var shadowOpacity: Float {
get {
return layer.shadowOpacity
}
set {
layer.shadowOpacity = newValue
}
}
@IBInspectable
var shadowOffset: CGSize {
get {
return layer.shadowOffset
}
set {
layer.shadowOffset = newValue
}
}
@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
}
}
}
}
这就是我得到的
答案 0 :(得分:44)
对于我来说,当我将鼠标悬停在InterfaceBuilder
Designables: Build Failed
上方时,
它给出了一条错误消息,提示类似
找不到声明或...的任何源文件。
因此,我得到了一个提示,例如Xcode无法索引我的自定义UIView
类文件,所以我要做的就是退出Xcode并重新启动它,然后它索引了我的自定义Swift类文件, InterfaceBuilder能够正确找到它。
首先尝试一下,然后转到其他选项!
答案 1 :(得分:6)
就我而言,我的故事板中的设计工作正常,直到我在视图中删除了一些不再需要我的用户界面的标签。
构建日志或DiagnosticReport文件夹中没有有用的消息。
我的修复是删除DerivedData文件夹(XCode" reboot")并重建项目。神奇地,故事板再次显示我的自定义旋钮! : - )
答案 2 :(得分:5)
它会失败,因为IBInspectable
的变量用于其他人IBDesignable
类
答案 3 :(得分:3)
我也偶然发现了这个问题,有两个建议可能会有所帮助:
#if targetEnvironment(simulator)
块中定义了一些类和函数,以至少通过构建。答案 4 :(得分:0)
我也在努力解决这个问题,然后删除项目派生数据并重新启动Xcode,然后才能再次运行。始终禁用“调试所选视图”,并且可设计的构建始终显示为失败。应用编译并运行良好。
答案 5 :(得分:0)
如果将CustomClass作为参考文件添加到项目中,则会发生这种情况。 例如,您可以在项目浏览器中看到蓝色文件夹,即参考文件夹。如果要将类复制到我们的项目中,请确保已选中“如果需要复制项目”。
就我而言,这就是问题所在。
答案 6 :(得分:0)
即使前两个步骤固定了地雷。 1.将类设置为DesignableView。 2.将模块设置为 3.清洁部件。 4.重新启动Xcode。
答案 7 :(得分:0)
确保您已在项目设置 -> 签名和功能选项卡中输入您的开发团队。如果没有开发团队,您将无法使用 @IBDesignable。但是,使用您的个人团队就足够了 - 只需要 AppleID。