IBDesignable 构建失败

IBDesignable Build Failed

我已经创建了 IBDesignableIBInspectable 自定义 class 来为视图提供阴影和角半径

但是当我将 Designable class 分配给 view 时,我得到 Designable Build Failed

这是我的代码

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
            }
        }
    }
}

这是我得到的

失败是因为 IBInspectable 的变量在别人的 IBDesignable class

中使用了

以下步骤解决了问题:

  1. 重命名 class
  2. 清理项目。
  3. Select 故事板,转到编辑器菜单并执行刷新所有视图,否则 select 自动刷新视图;等待构建完成。

在我的例子中,可设计对象在我的情节提要中工作正常,直到我在视图中删除了一些不再需要我的用户界面的标签。

构建日志或 DiagnosticReport 文件夹中没有有用的消息。

我的解决方法是删除 DerivedData 文件夹(Xcode "reboot")并重建项目。神奇的是,故事板再次显示了我的自定义旋钮! :-)

我也为此苦苦挣扎,然后删除了项目派生数据并重新启动 Xcode,然后它才可以再次工作。 "Debug Selected Views" 始终被禁用,可设计的构建始终显示为失败。应用已编译,运行 正常。

第一次尝试:IBDesignable 构建失败

对我来说,当我将鼠标悬停在 InterfaceBuilder Designables: Build Failed 上时, 它给出了一条错误消息,内容类似于

Cannot find any source files for the declaration or ...

所以,我得到了这样的线索 Xcode 无法索引我的自定义 UIView class 文件所以我所做的就是退出 Xcode并重新启动它,然后它为我的自定义 Swift class 文件编制了索引,InterfaceBuilder 能够正确找到它。

先试一试再看其他选项!

我也偶然发现了这个问题,有两个可能有用的建议:

  1. 在 "Report Navigator" (cmd-9) 中检查 "Interface Builder" 构建日志,它是与您的正常项目构建不同的构建,那里可能有有用的错误消息,而不仅仅是 "Build failed".
  2. Interface Builder 构建是为模拟器完成的,即 x86_64 架构。 iOS 的某些部分(例如我的 Metal)在模拟器上不存在,导致构建失败。我在 #if targetEnvironment(simulator) 块中定义了一些 类 和函数,以至少通过构建。

如果您的 CustomClass 作为参考文件添加到项目中,则会发生这种情况。 例如,您可以在 Project Explorer 中看到蓝色文件夹,即参考文件夹。如果您要将 类 复制到我们的项目中,请确保选择 'Copy items if needed'。

就我而言,这就是问题所在。

即使前两个步骤修复了我的问题。 1. 将 Class 设置为 DesignableView。 2.将模块设置为 3. 清理构建。 4. 重启Xcode.

确保您已在项目设置 -> 签名和功能选项卡中输入您的开发团队。没有开发团队就不能使用@IBDesignable。然而,使用您的个人团队就足够了 - 只需要 AppleID。