iOS - What Objective-C Methods Do I Need To Make IBInspectable Work?

This is the following code I have for a custom UIView class I made where you can control it's shadows in the interface builder:

#import "ShadowView.h"

@interface ShadowView ()

@property (copy, nonatomic) IBInspectable UIColor *shadowColor;
@property (assign, nonatomic) IBInspectable CGFloat xOffset;
@property (assign, nonatomic) IBInspectable CGFloat yOffset;
@property (assign, nonatomic) IBInspectable float shadowOpacity;
@property (assign, nonatomic) IBInspectable CGFloat shadowRadius;
@property (assign, nonatomic) IBInspectable CGFloat xInset;
@property (assign, nonatomic) IBInspectable CGFloat yInset;


@implementation ShadowView

#pragma mark - Lifecycle

//-(void)awakeFromNib {
//    [super awakeFromNib];
//    [self setUpView];

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

#pragma mark - Helpers

-(void)setUpView {
    self.layer.shadowColor = _shadowColor.CGColor;
    self.layer.shadowOffset = CGSizeMake(_xOffset, _yOffset);
    self.layer.shadowOpacity = _shadowOpacity;
    self.layer.shadowRadius = _shadowRadius;
    CGRect shadowRect = CGRectInset(self.bounds, _xInset, _yInset);
    self.layer.shadowPath = [[UIBezierPath bezierPathWithRect:shadowRect] CGPath];


When I use awakeFromNib, the shadows work on the iPhone 5 simulator but only half of them work on the iPhone6 emulator. drawRect seems to work for all, but I've seen Swift examples that use init(frame: CGRect) and init?(coder aDecoder: NSCoder). What are the needed methods for me to get this right all the time?

Note: I'm not using IB_DESIGNABLE in this case since shadows don't render in the interface builder.


When you use IBInspectable, you didn't need add a -(void)setupview method to set all property. you must add separate setter method like below: e.g.

-(void)setShadowColor:(UIColor)color {
     _shadowColor = color;
     self.layer.shadowColor = _shadowColor.CGColor;

hope it can help you.

Need Your Help

Are Matlab Matrices transferred pass-by-value or pass-by-reference?

matlab matrix pass-by-reference

I'm new at Matlab. You may find this question silly but I really wonder if the statement below is a pass-by-value operation or pass-by-reference operation.