@IBDesignable crashing agent

前端 未结 10 889
自闭症患者
自闭症患者 2020-11-29 02:59

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

  • Main.storyb
相关标签:
10条回答
  • 2020-11-29 03:35

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

    0 讨论(0)
  • 2020-11-29 03:37

    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

    0 讨论(0)
  • 2020-11-29 03:40

    There are a myriad of problems that can cause this. Fire up Console, and look for the crash report IBDesignablesCocoaTouch...

    I just sorted out a problem with a 3rd party designable which had issues with the valueForKey semantics.

    0 讨论(0)
  • 2020-11-29 03:46

    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:

    0 讨论(0)
  • 2020-11-29 03:47

    Xcode's Interface Builder requires that you implement both or neither initializers for @IBDesignable classes to render properly in IB.

    If you implement required init(coder aDecoder: NSCoder) you'll need to override init(frame: CGRect) as well, otherwise "the agent will crash" as seen in the errors thrown by Xcode.

    To do so add the following code to your class:

    override init(frame: CGRect) {
        super.init(frame: frame)
    }
    
    0 讨论(0)
  • 2020-11-29 03:49

    I found something really important when using an UIImage in any class marked @IBDesignable. The classic UIImage init would crash the agent:

    let myImage = UIImage(named: String) // crash the agent
    

    The solution is to use this init method of UIImage:

    let myImage = UIImage(named: String, in: Bundle, compatibleWith: UITraitCollection)
    

    Working code example:

    let appBundle = Bundle(for: type(of: self))
    let myImage = UIImage(named: "myImage", in: bundle, compatibleWith: self.traitCollection))
    

    Where self is your class with the @IBDesignable keyword. Xcode 9.4, Swift 4.1

    0 讨论(0)
提交回复
热议问题