ios - 添加到测试目标时的 IB 可设计错误

标签 ios swift xcode xctest ibdesignable

我有一个简单的 UIButton 子类,它使用 IBInspectable var 实现 IBDesignable:

@IBDesignable class Button: UIButton {
    @IBInspectable var borderColor: UIColor = UIColor.whiteColor() {
        didSet { layer.borderColor = borderColor.CGColor }
    }
}

我没有在框架内使用它,它按预期在 Interface Builder 中工作,但是,一旦我将这个子类添加到我的 Tests 目标,它就会停止实时渲染,我会收到以下错误:

Main.storyboard: error: IB Designables: Failed to update auto layout status: dlopen(TestTests.xctest, 1): Library not loaded: @rpath/XCTest.framework/XCTest
Referenced from: TestTests.xctest
Reason: image not found

Main.storyboard: error: IB Designables: Failed to render instance of Button: dlopen(TestTests.xctest, 1): Library not loaded: @rpath/XCTest.framework/XCTest
Referenced from: TestTests.xctest
Reason: image not found

如果我删除 IBDesignableIBInspectable 变量,错误就会消失 - 不幸的是,Interface Builder 中的实时渲染也是如此。

如何在没有这些错误的情况下针对 IBDesignable 类进行测试?

最佳答案

起初,我认为这是 Xcode 中的一种错误。以下是我找到的解决方法:

第一步

将您的类和属性标记为public

@IBDesignable public class Button: UIButton {
    @IBInspectable public var borderColor: UIColor = UIColor.whiteColor() {
        didSet { layer.borderColor = borderColor.CGColor }
    }

    @IBInspectable public var borderWidth:CGFloat = 0.0 {
        didSet { layer.borderWidth = borderWidth }
    }
}

第 2 步

从“测试”模块导入您的应用程序模块。

例如,假设您的应用程序名为 MyGreatApp,在您的 MyGreatAppTests/MyGreatAppTests.swift 中:

import UIKit
import XCTest
import MyGreatApp

class MyGreatAppTests: XCTestCase {

    func testExample() {
        let btn = Button()
        btn.borderColor = UIColor.redColor()
        XCTAssertEqual(UIColor(CGColor:btn.layer.borderColor), UIColor.redColor(), "borderColor")
    }
}

您不需要将“Button.swift”添加到“测试”目标。

第 3 步(针对 Swift)

在您的 Storyboard中,为任何自定义类显式选择模块 MyGreatApp,而不是让 Xcode 使用当前模块。

Interface Builder select main target module

关于ios - 添加到测试目标时的 IB 可设计错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26030951/

相关文章:

ios - 在 Swift 中从 Tableviewcell 中关闭 Popover ViewController

xcode - Swift 项目构建并运行但无法存档

objective-c - 如何在 Objective-C 单元测试中检查私有(private)成员?

ios - 如何在一个 Viewcontroller 上添加两个 CollectionView?

ios - 如何解决 ios 7 中 ui 标签栏的奇怪行为

ios - 如何用变量绑定(bind)窗体上的控件

ios - UITableViewCell 错误 : this class is not key value coding-compliant

ios - Textfield 确实结束了编辑崩溃应用程序

ios - Interface Builder 无法确定 "Main.storyboard"的类型。这可能是由于缺少 SDK

ios - Swift 可解码可选 key