Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

@IBDesignable class with .xib-File not rendering in Interface Builder

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!

like image 772
Marmelador Avatar asked Dec 05 '25 09:12

Marmelador


1 Answers

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
like image 135
Mthokozisi Avatar answered Dec 07 '25 16:12

Mthokozisi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!