IB_Designable

Pitfalls and Gotchas and Bears, oh my!

Chris Hulbert - splinter.com.au

End result

Contents

  • How to make one
  • How to use one
  • Use a commonInit
  • Pitfalls
  • Debugging

How to use it

Objective-C


#import <UIKit/UIKit.h>

IB_DESIGNABLE

@interface MyCustomView : UIView

@end
					

Drop into IB

Where did I come from, daddy?

  • Run time, manual creation
  • Run time, loading from xib
  • Design time

IB_DESIGNABLE Setup

Run time, manual Run time, xib Design time
initWithFrame initWithCoder initWithFrame
awakeFromNib KVC
prepareForInterfaceBuilder

Pitfall

That's right folks, IBDesignable doesn't use NSCoding

How to make designer babies


- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
#if !TARGET_INTERFACE_BUILDER
        [self commonInit]; // Run time, manual creation.
#endif
    }
    return self;
}
- (void)awakeFromNib {
    [super awakeFromNib];
    [self commonInit]; // Run time, loading from xib.
}
- (void)prepareForInterfaceBuilder {
    [super prepareForInterfaceBuilder];
    [self commonInit]; // Design time.
}
					

Why prepare For Interface Builder?

Why not call from initWithFrame? 2 birds with one stone: manual run time + design time.

KVC of IB properties occurs after initWithFrame, so your settings eg backgroundColor would be clobbered.

Common code


@implementation MyCustomView {
    UILabel *_label;
}

- (void)commonInit {    
    _label = [[UILabel alloc] init];
    _label.text = @"TEST";
    [self addSubview:_label];
}

- (void)layoutSubviews {
    [super layoutSubviews];
    _label.frame = self.bounds;
}

@end
					

When things go bad

IB's gone full debug

Images and other Resources

[UIImage imageNamed:] won't work, as it runs from a framework, no bundle context. Instead:


[UIImage imageNamed:@"MyImage" 
           inBundle:[NSBundle bundleForClass:[self class]]
    compatibleWithTraitCollection:nil];
					

The end

So please remember:

  • How to use and insert into IB
  • Use a commonInit
  • Doesn't use NSCoding, it uses KVC
  • Careful with bundle resources
  • Debug from Editor menu

Chris Hulbert - splinter.com.au