IBDesignable Errors When Adding to Tests Target

前端 未结 6 1508
青春惊慌失措
青春惊慌失措 2020-12-14 08:12

I have a simple UIButton subclass that implements IBDesignable with an IBInspectable var:

@IBDesignable class Button:          


        
6条回答
  •  独厮守ぢ
    2020-12-14 08:42

    At first, I thought this was a kind of bug in Xcode. Following is the workaround I found:

    STEP 1

    Mark your class and properties as 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 }
        }
    }
    

    STEP 2

    Import your application module from your "Tests" module.

    For example, assuming that your application is named MyGreatApp, in your 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")
        }
    }
    

    You don't need to add 'Button.swift' to your "Tests" target.

    STEP 3 (for Swift)

    In your storyboard explicitly select the module MyGreatApp for any custom classes instead of letting Xcode use the current module.

    Interface Builder select main target module

提交回复
热议问题