IB Designables Error when using UnitTests

965 views Asked by At

I am currently working on an iOS App using Xcode 6.1.1 and Objective-C. In the app there are several custom view subclasses. One of them is a subclass of UIButton, another one is a subclass of UIView.

Those custom views are marked as IB_DESIGNABLE and have several properties that are IBInspectable.
I also use Unit Tests in the Xcode project. Every time I open the storyboard Xcodes gives me several errors.

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

IB Designables 
Failed to render instance of CustomRadioButton: dlopen(UnitTests.xctest, 1): Library not loades: @rpath/XCTest.framework/XCTest 
Referenced from UnitTests.xctest
Reason: image not found

When I remove the IB_DESIGNABLE statement the errors go away. Unfortunately, I need them to be IB_DESIGNABLE. I found a post on StackOverflow that has the same question but is using Swift. The suggested solution does not work using Objective-C, hence the features used to resolve the problem do not exist (as far as I know) in Objective-C.

Here is the link to the question: IBDesignable Errors When Adding to Tests Target

Does anybody have an idea on how to fix this?
I also tried using Xcode 6.2 beta, but the problem persists.

As one comment requested, here is the code to one of my custom views:

CustomButton.h #import

@interface CustomButton : UIButton

@property (nonatomic, strong) UIFont *titleFont UI_APPEARANCE_SELECTOR;
@property (nonatomic, assign) IBInspectable BOOL useShapedForm;

@property (nonatomic, strong) IBInspectable UIColor *defaultColor;
@property (nonatomic, strong) IBInspectable UIColor *selectedColor;
@property (nonatomic, strong) IBInspectable UIColor *disabledColor;

@property (nonatomic, assign) IBInspectable CGFloat cornerRadius;

- (void)setDefaults;

- (IBAction)touchDown:(id)sender;
- (IBAction)handleButtonClick:(id)sender;

@end

CustomButton.m

#import "CustomButton.h"

@implementation CustomButton

#pragma mark - Initialisation

- (instancetype)init
{
    if (self = [super init]) {
        [self setDefaults];
    }
    return self;
}


- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        [self setDefaults];
    }
    return self;
}


- (id)initWithCoder:(NSCoder *)aDecoder
{
    if (self = [super initWithCoder:aDecoder]) {
        [self setDefaults];
    }
    return self;
}


- (void)setDefaults
{
    self.useShapedForm = NO;

    self.cornerRadius = self.useShapedForm ? 5 : 0;

    self.defaultColor = self.useShapedForm ? [UIColor rsm_bg_btn_2] : [UIColor clearColor];
    self.selectedColor = self.useShapedForm ? [UIColor rsm_bg_btn_1] : [UIColor clearColor];
    self.disabledColor = self.useShapedForm ? [UIColor rsm_bg_btn_3] : [UIColor clearColor];

    if (!self.useShapedForm) {
        [self setTitleColor:[UIColor rsm_font_333_dark] forState:UIControlStateNormal];
        [self setTitleColor:[UIColor rsm_bg_red] forState:UIControlStateSelected];
        [self setTitleColor:[UIColor rsm_font_333_light] forState:UIControlStateDisabled];
    } else{
        [self setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    }
}


#pragma mark - Properties

- (void)setTitleFont:(UIFont *)titleFont
{
    if (self.titleFont != titleFont) {
        self.titleFont = titleFont;
        [self.titleLabel setFont:self.titleFont];
    }
}

- (void)setTitleEdgeInsets:(UIEdgeInsets)titleEdgeInsets
{
    UIEdgeInsets insets = UIEdgeInsetsMake(titleEdgeInsets.top - 10,
                                           titleEdgeInsets.left,
                                           titleEdgeInsets.bottom,
                                           titleEdgeInsets.right);
    [super setTitleEdgeInsets:insets];
}

- (CGSize)intrinsicContentSize
{
    CGSize defaultMetric = [super intrinsicContentSize];
    // TODO: Metrics anpassen!!
    return CGSizeMake(defaultMetric.width, 39);
}


#pragma mark - Private Methods

- (void)drawRect:(CGRect)rect
{
    [super drawRect:rect];

    if (self.useShapedForm) {
        [self drawRSMShape];
    }
}


#pragma mark - Public Methods

- (void)drawRSMShape
{
    UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:self.bounds
                                               byRoundingCorners:UIRectCornerTopRight | UIRectCornerBottomRight | UIRectCornerBottomLeft
                                                     cornerRadii:CGSizeMake(self.cornerRadius, self.cornerRadius)];
    [path closePath];

    if (self.selected) {
        [self.selectedColor setFill];
    }
    else if (self.enabled){
        [self.defaultColor setFill];
    }
    else {
        [self.disabledColor setFill];
    }

    [path fill];
}


- (IBAction)touchDown:(id)sender
{
    RSMButton *button = (RSMButton *)sender;
    button.highlighted = NO;
}


- (IBAction)handleButtonClick:(id)sender
{

}

@end
1

There are 1 answers

0
RasTheDestroyer On

I was having this issue too. It was caused by the IB Designable file having target membership in both the project and the test projects. Once I removed it from the test project, cleaned and restarted Xcode, the error went away