IB Designables: Failed to render and update auto layout status
Asked Answered
C

14

35

I have a custom view (xib) that has a UIButton inside of it, I made id IBDesignable doing the following:

UserView.swift

import UIKit

@IBDesignable
class UserView: UIView {

    @IBOutlet var view: UIView!
    @IBOutlet weak var userButton: UIButton!

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

    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        load()
    }

    fileprivate func load() {
        Bundle.main.loadNibNamed("UserView", owner: self, options: nil)
        addSubview(view)
        self.frame = bounds
    }

}

UserView.xib

  • Added the UIButton and set constraints
  • Set the File's Owner: UserView

Storyboard

I added a UIView to a Bar Button Item and assigned UserView class but nothing is rendering, and I got the following build error:

error: IB Designables: Failed to render and update auto layout status for FoodViewController (bR3-Kz-wX7): The agent threw an exception.

My currently environment: Xcode 9, Swift 4

Congius answered 13/10, 2017 at 6:22 Comment(1)
Try to disable 'Use Trait Variations' (Identity and Type panel) for any xib file that you might have for custom views that are used in your storyboard. from #39732812Burgos
T
44

I just had the exact same issue, and the only thing that worked for me was instead of using the main Bundle I had to get the Bundle for the Nib I wanted. Essentially changing:

Bundle.main.loadNibNamed("UserView", owner: self, options: nil)

To:

let bundle = Bundle(for: UserView.self)
bundle.loadNibNamed("UserView", owner: self, options: nil)

Which is bizarre, as in my case when comparing the two Bundles at runtime in LLDB they were identical (class name is different but the rest of my setup was identical to the OP)

lldb screenshot Updated

Why loading NIB from main bundle does not work?

Because when InterfaceBuilder is rendering it is not running the application. There is no "main application bundle".

Trula answered 16/5, 2018 at 11:2 Comment(4)
This worked for me, does anyone know why loading NIB from main bundle does not work?Freda
Because when InterfaceBuilder is rendering it is not running the application. There is no "main application bundle".Parimutuel
@Parimutuel Now that you say it, it makes so much sense! Why isn't this in the documentation, or at least in Xcode Help where they actually introduce @IBDesignable?!Albur
BRILLIANT ANSWER :)Ancestral
M
35

I add this script at the end of my Podfile and performed pod install again. So I could build normally my project.

post_install do |installer|
    installer.pods_project.build_configurations.each do |config|
        config.build_settings.delete('CODE_SIGNING_ALLOWED')
        config.build_settings.delete('CODE_SIGNING_REQUIRED')
    end
end
Mule answered 25/5, 2018 at 1:42 Comment(2)
this worked for me as well. I just curious what the problem is and what exactly this code does. Will it impact anything else in my project?Salinometer
It worked for me too. It just relaxes the requirement of code signing requirement for the frameworks you want to load via Cocoapods.Schizont
P
6

This question is related to this answer. Check both.
Failed to render instance of ClassName: The agent threw an exception loading nib in bundle

Bottom Line

Make sure that your xib file does not have any orphaned outlets. Check each view/element of the nib for an outlet and remove and re-add.

A nib subview with an outlet

For me, they were orphaned outlets. It seems the nibs and IB are very finicky and the debugger doesn't give many details... if you can't get this to work, you might start with a fresh nib/xib file.


Walkthrough of my working code

This issue took me several days to resolve. For the benefit of others I am giving a verbose answer and showing all of my relevant code and connections.

A working commit where I implemented the fix action (above) is here: https://github.com/jfosterdavis/ZMAppFoundation/commit/6855f0d5b9cd1bc320395e57e2b271653ef7acd1

Xcode version 9.2

My file structure

Here is my file structure (note: I am creating a pod with cocoapods named ZMAppFoundation. Also, ZMPieTimerView.swift is not relevant to the solution.):

enter image description here

I want my nib to be viewable (IBDesignable) in the Main.storyboard file. Here is the File's Owner and Custom Class of my xib, ZMGauge.xib:

File's Owner Custom Class

Code, implementations

Here is my ZMXibView class:

//  Adapted from https://medium.com/zenchef-tech-and-product/how-to-visualize-reusable-xibs-in-storyboards-using-ibdesignable-c0488c7f525d

import UIKit

@IBDesignable
open class ZMXibView: UIView {
    
    
    var contentView:UIView?
    @IBInspectable var nibName:String?
    
    override open func awakeFromNib() {
        super.awakeFromNib()
        xibSetup()
    }
    
    func xibSetup() {
        guard let view = loadViewFromNib() else { return }
        view.frame = bounds
        view.autoresizingMask =
            [.flexibleWidth, .flexibleHeight]
        addSubview(view)
        contentView = view
    }
    
    func loadViewFromNib() -> UIView? {
        guard let nibName = nibName else { return nil }
        let bundle = Bundle(for: type(of: self))
        let nib = UINib(nibName: nibName, bundle: bundle)
        return nib.instantiate(
            withOwner: self,
            options: nil).first as? UIView
    }
    
    override open func prepareForInterfaceBuilder() {
        super.prepareForInterfaceBuilder()
        xibSetup()
        contentView?.prepareForInterfaceBuilder()
    }
}

In storyboard, my xib is designated in the Custom Class. I am using cocoapods to develop a pod. I have explicitly selected the Module. (Note that in my XibView class, I have to define the nibName property.):

Setting custom class on the xib

Don't Forget!

Then you may need to clean, close Xcode, rebuild, or simply wait a while for the IB to render it again. It is still a mystery to me exactly when Xcode will decide to try to render the IBDesignables, and I don't know of a way to force.

Success

Based on my code and the actions I took, my nib is now IBDesignable viewable in my Main.storyboard.

nib is viewable now

In case you missed it

In case you missed it, my solution is at the very top of this post. I got mine to clear the error cited by the OP by removing some orphaned IBOutlets.

Petronilapetronilla answered 22/1, 2018 at 18:33 Comment(0)
B
6

Xcode 9.3, CocoaPods 1.5.3 It may be due to recent update on CocoaPods version. See issue here.

I had this error IB Designables: Failed to render and update auto layout status in the storyboard, the class STPPaymentCardTextField in Stripe SDK related to Cocoapods.

Solution is to downgrade your CocoaPods to 1.4.0.

sudo gem list cocoapods

sudo gem uninstall cocoapods

sudo gem install cocoapods -v 1.4.0

pod update
Bogey answered 6/7, 2018 at 22:39 Comment(2)
After all steps remove derived data and restart xcode . its works for meEcumenical
I was ignored this answer at a glance, since I use Carthage for current project. However, this affects project whether it contains Podfile or notTransom
A
4

For me, the fix came down to getting Interface Builder to redraw the object. I had this in two places and both were fixed by no changes to code but the symptoms of the agent error and the views being blank in storyboard was eventually fixed.

One was fixed by dragging a UIView to a different order under the View and rebuilding. A second in a TableView with Prototype cell was fixed by selecting the TableView, changing the count of Prototype cells to two, wait a sec, then change it back to one. The TableView, which had been a white blank, then drew all the elements correctly and the agent error was gone.

It feels like it's a bug with IB and from other searches, many solutions exist for the similar error. Hope this helps someone.

Aphotic answered 15/10, 2017 at 1:24 Comment(1)
This answer is still relevant in Xcode 11.2.1. I was able to simply increase the number of prototype cells in a table view form Attributes Inspector, the status bar at the top showed a process being started (the process finished very quickly), when it finished I then brought the prototype cells back to the original number and the view controller appeared again.Acicular
J
3

(xCode 9.2, Swift 4) The following may not help your exact problem but may help others with a very similar problem. This was my error:

error: IB Designables: Failed to render and update auto layout status for CustomExampleView (XXXXX): the agent crashed

I was able to remove the error when deleting the CustomExampleView.xib and CustomExampleView.swift. To replicate, I then re-created the files, went to the .xib and set the top level view's custom class to CustomExampleView.

My mistake was setting the custom class to the top-level view on the .xib instead of setting the custom class on the File's Owner. After setting the proper custom class, my problem was solved.

Jackshaft answered 7/3, 2018 at 15:12 Comment(1)
After looking at the more complicated answers above, this simpler solution turned out to be the fix for me. If you are not as advanced in Swift this is likely your problem. The File's Owner mentioned above is right below "Placeholders" in the .xib view, and right above the View icon tree, etc. For some reason in older versions of Swift this didn't matter, but it now causes this error.Foreordain
P
3

I'm cringing at how dumb this is that doing what I'm about to tell you makes a difference, but you can try removing the breaking code out of

override func prepareForInterfaceBuilder() {

and only use it in

override func awakeFromNib() {

. I'm cringing, even more, when I report that its possible that your changes could still reflect in storyboard as desired when doing this.

You can identify the breaking code by commenting out all the lines and using a process of elimination to identify the breaking line.

Proudlove answered 19/9, 2018 at 7:34 Comment(2)
This turned out to be the solution to my problem, though the answer itself is badly written and I overlooked it at first. In short, I had code in my init function which only made sense at runtime (and not design time). Moving it into awakeFromNib was the correct solution for me.Bleach
Hey @JohnJ.Camilleri, welcome to StackOverflow! If you'd like to edit an answer you can push the edit button under the answer, I bet you have some good edits to suggest.Proudlove
D
2

For me this problem solved by,

  1. Updating pods.
  2. Changing build setting to '$(inherited)' for swift libraries embedded option.
  3. Building, cleaning, building.
  4. Closing project.
  5. Quitting Xcode.
  6. Running again.

hahaha old hack :) Thanks!

Dissatisfactory answered 21/6, 2018 at 11:17 Comment(1)
hmm...yes, cleared derived data, closed xcode, updated pods and opened xcode. worked for me too !!!Dealings
S
2
    post_install do |installer|
    installer.pods_project.build_configurations.each do |config|
        config.build_settings.delete('CODE_SIGNING_ALLOWED')
        config.build_settings.delete('CODE_SIGNING_REQUIRED')
    end
end

Entering the above-mentioned code in the Podfile solves the issues. This code just does away with the requirement of code signing for the installed framework via Cocoapods. But this seems to be a bug in IBDesignable too as using Xcode 10 bets 4 I don't get this bug due to new build system incorporated in Xcode 10.

Schizont answered 27/7, 2018 at 6:48 Comment(0)
M
1

It is the problem with some cocoa pods versions like 1.5.0. If you are using this one then it cause to rendering failed to move from current version to other run this commands in terminal

sudo gem uninstall cocoapods

You can install any specific version by mentioning like this

sudo gem install cocoapods -v 1.4.0

Hope it will works for you.

Muezzin answered 18/6, 2018 at 8:28 Comment(2)
Its not working for me. The issues are happening when i work in xcode 9.4.1 . Its ok if i work in xcode 9.3.Carolanncarole
try this Xcode -> preferences -> general and check off live issueMuezzin
A
0

Unfortunately, you can't use IBOutlets within a IBDesignable. Better answer for you here:

Live Render IBOutlet Connected Subviews Via IBInspectable Properties

Perhaps you wanted to have your UserView extend UIButton, rather than UIView?

Azide answered 13/10, 2017 at 8:32 Comment(0)
M
0

I don't really know much about the cause of this, but I've seen this a couple of times and every time it's fixed by just restarting xcode. I think that's where everyone should start and if that doesn't work then you can try the other suggested answers.

Maffei answered 3/7, 2018 at 11:15 Comment(0)
H
0

XCode 11.6

If you are using a UITableView & prototype cells, Select UITableView and in the attributes inspector, simply increase the count of Prototype cells. Then xcode will automatically build it again & will fix the issue. Once it is solved,reduce the count to previous value. enter image description here

Hermelindahermeneutic answered 24/8, 2020 at 14:48 Comment(0)
C
-2

I disabled the 'User trait Variations' and my problem was solved.

Champac answered 15/1, 2018 at 5:9 Comment(1)
It doesn't help here. We should apply another steps. (Like pods post_install and Erasing the Derived Data)Crepe

© 2022 - 2024 — McMap. All rights reserved.