0

I have a UIView class where I create some buttons and labels programmatically, and I have a view inside a certain ViewController that is attached to this class. The class works perfectly fine as expected, but whenever I try to link this custom UIView class to a xib view i get these errors:

-"Main.storyboard: error: IB Designables: Failed to update auto layout status: Interface Builder Cocoa Touch Tool crashed"

-"Rendering the view took longer than 200 ms. Your drawing code may suffer from slow performance.
"

-"error: IB Designables: Failed to render instance of MyView: Rendering the view took longer than 200 ms. Your drawing code may suffer from slow performance.
"

Here is the initialization code in the UIView class:

@IBDesignable class MyView: UIView {

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

        setup()

    }

 required init(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)

        setup()

    }

func loadFromNib() -> UIView {

        let bundle = NSBundle(forClass: self.dynamicType)
        let nib = UINib(nibName: "MyView", bundle: bundle)

        let view = nib.instantiateWithOwner(self, options: nil)[0] as! UIView

        return view

    }


    func setup() {

        var view = loadFromNib()
        view.frame = bounds
        view.autoresizingMask = UIViewAutoresizing.FlexibleWidth | UIViewAutoresizing.FlexibleHeight
        addSubview(view)


    }

}

Both the class and the xib file have the same name,

UPDATE: I found out that the:

 required init(coder aDecoder: NSCoder) {
            super.init(coder: aDecoder)

            setup()
        }

Is the one responsible for all the errors, but if I deleted it's content and ran the app the view doesn't show as expected, I also tried awakeFromNib() and same issues happened.

Abdou023
  • 1,654
  • 2
  • 24
  • 45

1 Answers1

0

Not quite sure what is causing your specific errors, but here is what helped me when I was having trouble connecting a UIView in a xib file to a custom UIView class:

1) Delete the contents of your DerivedData sub-folder:

(The contents of DerivedData will be recreated when you build your projects again.)

Your folder can be found by visiting the "Locations" tab in your XCode preferences. Just delete the folders inside and re-build / run your project again.

2) Clean your project

Product -> Clean (Or just use the [command + shift + K] command.

3) Restart xcode and re-build / run your code.

Hope this helps!

ecatalano
  • 687
  • 1
  • 4
  • 17