我有一个简单的 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
如果我删除 IBDesignable
和 IBInspectable
变量,错误就会消失 - 不幸的是,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 使用当前模块。
关于ios - 添加到测试目标时的 IB 可设计错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26030951/