4

I have a CustomClass.swift and a CustomClass.xib. I want XCode's Interface Builder to render views of class CustomClass using the provided .xib-File. Just like it does when I run the app.

I am using XCode 8.3.2 (8E2002)

CustomClass.swift

import UIKit


@IBDesignable class forceInterface: UIView {

    override init(frame: CGRect) {
        super.init(frame: frame)
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        Bundle.main.loadNibNamed("forceInterface", owner: self, options: nil)
        self.addSubview(view)
        view.frame = self.bounds

    }



    @IBOutlet var view: UIView!
    @IBOutlet weak var label: UILabel!
    @IBOutlet weak var progressView: UIProgressView!

    override func prepareForInterfaceBuilder() {
        super.prepareForInterfaceBuilder()

    }
}

CustomClass.xib

File owner is set to CustomClass. It contains a UIView that contains a UISwitch and a UILabel. CustomClass.xib

The problem is that, eventhough I have @IBDesignable in my CustomClass.swift, it isn't rendering in the InterfaceBuilder. I am placing a UIView there and setting its class to CustomClass: enter image description here

As you can see, it even says Designables: Up to date in the inspector. But I am not seeing anything.

When running the app everything works as expected!

Marmelador
  • 947
  • 7
  • 27

1 Answers1

1

Why are you doing this

self.addSubview(view)
view.frame = self.bounds

You should assign the views' frame before you add it as a subview to its superview

view.frame = self.bounds
self.addSubview(view)

I am not sure why you view is not loading even though you have assigned it as IBDesignable. I normally use these steps to troubleshoot.

  • Close and reopen Xcode
  • Clean Xcode
  • Wait for a while, normally around a minute or so, and you will see the view refresh/update itself, displaying your custom drawn content
Mthokozisi
  • 171
  • 2
  • 12