@IBDesignable crashing agent

2019-01-13 11:51发布

When I write my own UIButton-extended class and make it @IBDesignable, I receive two errors in Interface Builder, namely:

  • Main.storyboard: error: IB Designables: Failed to update auto layout status: The agent crashed because the fd closed
  • Main.storyboard: error: IB Designables: Failed to render instance of RandjeUIButton: The agent crashed

Here is my code:

import UIKit

@IBDesignable
class RandjeUIButton: UIButton {
    required init(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)

        self.backgroundColor = UIColor.blackColor()
    }
}

I am working in Xcode 7 beta 2 on OS X 10.11 beta 2. (Running in VM)

9条回答
Lonely孤独者°
2楼-- · 2019-01-13 12:26

In Xcode 7.3, none of the above solutions worked for me, but the accepted answer to this question did: Failed to render instance of IB Designables

  1. Clear Xcode derived data for the project. They are in ~/Library/Developer/Xcode/DerivedData
  2. Clean your current build by pressing ⌘⇧K
  3. Build your project
  4. In storyboard go to Editor menu and do Refresh All Views; wait for build to be completed and errors should be gone

I did not need to do the 4th step to solve the problem (and get my PaintCode-drawRect'd UIView to paint in the storyboard), included here just in case.

Credit to @Mojtaba and @WeZZard

查看更多
forever°为你锁心
3楼-- · 2019-01-13 12:26

This is how I solves this problem:

  • Make sure the outlets are properly initialized
  • @IBInspectable properties would be properly initialized
  • in xib file, click on File's owner placeholder, Go to Identity inspector, make sure it wont have any default values

import UIKit

@IBDesignable class TestView: UIView {
    
    @IBOutlet weak var label: UILabel!
    
    
    @IBInspectable var textLabel1: String? {
        get {
            return label.text
        }
        set {
            label.text = newValue
        }
    }

    
    // MARK: Setup
    
    var view1: UIView!
    var nibName: String = "TestView"
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        xibSetup()
    }
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        xibSetup()
    }
    
    private func xibSetup() {
        view1 = loadViewFromNib()
        
        view1?.frame = self.bounds
        view1?.autoresizingMask = [.FlexibleWidth, .FlexibleHeight]
        
        if view1 != nil {
            addSubview(view1!)
            //textLabel1 = "ok"
        }
    }
    
    private func loadViewFromNib() -> UIView? {
        let bundle = NSBundle(forClass: self.dynamicType)
        let nib = UINib(nibName: nibName, bundle: bundle)
        for object in nib.instantiateWithOwner(self, options: nil) {
            if let view: UIView = object as? UIView {
                return view
            }
        }
        return nil
    }
    
    
}

Usage:

enter image description here

查看更多
Bombasti
4楼-- · 2019-01-13 12:33

I wasted a full day on this and finally I got my problem solved enter image description here

I selected Build for target as 8.0 and it fixes everything for me.

查看更多
Rolldiameter
5楼-- · 2019-01-13 12:34

I've met the same problem and solved it this way:

  1. The Problem:

error: IB Designables: Failed to update auto layout status: The agent crashed

  1. Find the debug button in the inspection file and click it.

debug

  1. Then the Xcode will tell you where the prolem is. In my case I drop the IBDesignable before the class.

  2. Then I clean and rebuild it, the error disappeared

查看更多
Melony?
6楼-- · 2019-01-13 12:38

Just re-open a code on another Xcode setup System. In my case it Worked.

查看更多
爱情/是我丢掉的垃圾
7楼-- · 2019-01-13 12:41

For me, it was not using #if !TARGET_INTERFACE_BUILDER that got me. Basically I had some code that would result in accessing a path in my Bundle...

Bundle.main.path(forResource: "Foo", ofType: "plist")

The problem is that when running in IB (and not in your app), Bundle.main isn't your app...

(lldb) po Bundle.main
NSBundle </Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/Library/Xcode/Overlays> (loaded)

So the answer to this is simply to review your @IBDesignable UIView code carefully and use #if !TARGET_INTERFACE_BUILDER for anything (in my case calls to CoreLocation for example) that doesn't make sense when running at design time.

How I debugged and found this

This is what you see when using the Editor -> Debug Selected View while selecting your @IBDesignable UIView in your storyboard:

This is what you see when using the <code>Editor -> Debug Selected View</code> while selecting your <code>@IBDesignable UIView</code> in your storyboard

You'll then crash at the right location

The Debug Navigator

In my case, as you can see initXXXX was doing an assert, which was crashing at design time, because it was looking for a value in file in my Bundle — which is Xcode, at design time.

查看更多
登录 后发表回答