@IBDesignable crashing agent

前端 未结 10 890
自闭症患者
自闭症患者 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:53

    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

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

    I wasted a full day on this and finally I got my problem solved

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

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

    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:

    You'll then crash at the right location

    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.

    0 讨论(0)
  • 2020-11-29 04:00

    XCode 10, Swift 4.2

    I found an answer here

    Solution was simple - changing the way the bundle was resolved in the required init?(coder aDecoder: NSCoder) method of my custom view class.

        Bundle.main.loadNibNamed(String(describing: TestView.self), owner: self, options: nil)
    

    needed to be changed to

    let bundle = Bundle(for: TestView.self)
    bundle.loadNibNamed(String(describing: TestView.self), owner: self, options: nil)
    
    0 讨论(0)
提交回复
热议问题