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
I disabled the 'User trait Variations' and my problem was solved.
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.
(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:
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.
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.
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.
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.
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.):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
:Code, implementations
Here is my ZMXibView class:
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.):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.
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.
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.