16

I'm using storyboards for the first time in iOS 8 and so far have been loving the live rendering aspect of things on the storyboard. However, I seem to have hit a snag in getting my views to render properly on the storyboard.

I have a container UIView that contains a connection to a UILabel on the storyboard, I am attempting to set the label's text based on an IBInspectable attribute on the label's parent container view.

@IBDesignable class ContainerView : UIView {
    @IBOutlet weak var : titleLabel : UILabel!

    @IBInspectable var title : String = "" {
        didSet {
            titleLabel?.text = title
        }
    }

    /* Init functions */

    prepareForInterfaceBuilder() {
        self.titleLabel?.text = title
    }
}

If I set the attribute in the storyboard it renders as expected while the program is executing but fails to render in the storyboard as I would expect. I've checked my connections and everything appears to be hooked up properly.

My question is: Is it possible to affect the contents of an IBOutlet connected view via IBInspectable attributes and have them live render on the storyboard, and if so, what am I missing or doing wrong?

AstroCB
  • 12,337
  • 20
  • 57
  • 73
Matt C.
  • 689
  • 5
  • 14
  • I know that all outlets are nil when prepareForInterfaceBuilder is called. I'm sure that is a big reason why the live rendering isn't propogated, but I'm not sure if there is anywhere else I can declare the behavior I want in order to get it to show up in Interface Builder. – Matt C. Oct 13 '14 at 20:57

1 Answers1

14

Unfortunately you can't see IBOutlet objects in interface builder for your custom views which are marked as IBDesignable. If you want to see your outlets in interface builder, you have to use regular variables instead IBOutlet and you have to create your objects programmatically.

Also please note that, if you need to change something from interface builder for your objects, you have to define your properties as IBInspectable. Currently following variables types are valid for IBInspectable:

Bool, CGFloat, CGPoint, CGRect, CGSize, NSInteger, NSString, UIColor, UIImage

I hope this answer is adequately clear for you.

Edit: I found following article which is describing a way how to do what you need:

http://justabeech.com/2014/07/27/xcode-6-live-rendering-from-nib/

2nd Edit: I tried the article and it works. Now I can see my outlets on interface builder

Integer
  • 548
  • 4
  • 11
  • Yeah this is what I thought, but I wanted to get confirmation. I was trying to keep all of my UI design in the storyboards, but I guess that just isn't possible at this time. Thanks for the response. – Matt C. Nov 12 '14 at 14:28
  • is it possible to use a didSet or sth to update customView in storyboard when change inspectableFiled? like this: @IBInspectable var title: String? { didSet { lblTitle.text = title } } – Mahdi Moqadasi Jul 21 '20 at 14:53