Could not instantiate class named IBNSLayoutConstraint
Asked Answered
E

8

89

I'm using XCode6 beta and trying out Swift. When I put some auto layout constraints in a view controller the app crashes with the following error: Terminating app due to uncaught exception 'NSInvalidUnarchiveOperationException', reason: 'Could not instantiate class named IBNSLayoutConstraint'

Expressivity answered 7/6, 2014 at 8:25 Comment(1)
Not much code to put here :) The view controller is a regular freshly created UIViewController with a couple of outlets and the regular viewDidLoad and didReceiveMemoryWarning. And then there are the constraints in Interface Builder. I guess I can add the view controller code here.Expressivity
S
240

You're getting this error because you've set a constraint to an IBOutlet that is removed at runtime. This happens when you set the constraint to be a placeholder in Interface Builder. Since the constraint is removed, when it goes to unarchive it, it throws an error saying it can't do so.

There are two ways to correct this.

Method 1

  1. Right-click on your Storyboard > Open As > Source Code
  2. In the opened storyboard xml, search for placeholder="YES".
  3. You'll find constraints that are set to be removed at runtime. Remove the placeholder attribute from the constraint, save and close.
  4. Run the app and your problem should be fixed.

Method 2

  1. Find the constraint that's causing your problems in Interface Builder. Uncheck the Placeholder option in the GUI. This should be one of the constraints that's set to an IBOutlet in the ViewController that's causing your crash.

Interface Builder attribute editor showing the Placeholder option checked.

This is what it should look like:

Interface Builder attribute editor showing the Placeholder option unchecked.

Alternative

Assuming you actually want the constraint to be a placeholder, then you'll need to remove any referencing outlets. To do this, select the constraint that you wish to be a placeholder. Then open the connections inspector (the button furthest to the right that looks like this: (->) ) and then remove any referencing outlets that may exist on that constraint.

Suave answered 11/6, 2014 at 12:28 Comment(7)
What if you want the constraint to be a placeholder? developer.apple.com/library/ios/documentation/UserExperience/…Providing
@DanielGalasko If you want it to be a placeholder, then don't assign it to an IBOutlet.Suave
@SandyChapman That makes sense! Thanks a lot Sandy, could you include that in your answer so I can upvote?Providing
@DanielGalasko, Sure thing. It is a good point, my answer was making the assumption that you wanted the constraint on an IBOutlet.Suave
@SandyChapman : Is there a way to find out which constraint is causing this? My project is too big to be able to go through each one and the stack dump is not pin pointing which constraint. Also, I tried to look for placeholder="YES" but couldn't find any in the xml.Delapaz
@Delapaz : If you're getting this exception, there should be a placeholder="YES" in the xml. The storyboard format hasn't changed. Have you tried cmd+F and entering placeholder="YES" exactly when viewing the storyboard xml?Suave
@SandyChapman: Thank you very much for the quick response. Yes I am viewing the xml (right clicking and view as source code), and I can't find any thing for placeholder="YES". I have posted more details about this at : #34191732Delapaz
T
6

I had the same problem just now and the following worked for me.

I released a working version of my app to the App Store, came back to work on it again a few days later tapping onto one my tabs in the UITabBarController it crashed with the error:

Terminating app due to uncaught exception 'NSInvalidUnarchiveOperationException', reason: 'Could not instantiate class named IBNSLayoutConstraint'

I didn't have any placeholder layout constraints that I was aware of or constraint outlets that shouldn't have been defined.

The solution for me was to simply turn size classes off and keep size class data for iPhone (App is only for iPhone). This must've deleted anything I'd missed in the size classes. I want size classes so I turned them back on and the app just worked again.

Thimblerig answered 6/1, 2016 at 16:56 Comment(2)
Thank you for your post! Just experienced the same thing, I didn't changed anything in my app. In my case it crashed on loading a 'UIPageViewController'. Turning off size classes and re-enabeling them solved the error.Molliemollify
Thank you!! Same experience as nor0x. Very strange, but very wonderful to find the solution here.Conjectural
A
4

This might not be necessarily a constraint problem. For me it was caused by not having checked "Installed" for a size class that was applying to my layout, see here install must be checked

Ashmore answered 9/1, 2016 at 9:16 Comment(0)
E
1

There was some problem with using size classes. I disabled that option from the storyboard properties and use only auto layout. It wasn't such a big problem since the app is iPhone only.

Expressivity answered 7/6, 2014 at 12:11 Comment(1)
Disabling the "Size Classes" & running fixed it for me as well. I then re-enable the size classes, and it kept working.Luing
S
1

What ended up happening to me was that I cut a subview inside of Interface Builder to copy it into another xib. This left the subview in original xib Interface builder with it being greyed out. After removing the subview from it's original place (by selecting and deleting). I was able to get rid of the error.

Scrubland answered 9/2, 2016 at 18:12 Comment(0)
G
0

There is an option in the inspector window when you select the xib file that will not use the constrains under "Show file the inspector" in the inspector called "Use autolayout".

Gehenna answered 7/6, 2014 at 9:4 Comment(3)
I know of that one, but I want to use autolayout for that storyboard.Expressivity
@Expressivity 0- #22466920Gehenna
Thanks. I tried playing around with those but it didn't really help since I was using size classes as well. But I'll read through this and see if I missed something. Thanks again.Expressivity
C
0

I made a change to a scroll view so that it would let the picker controls embedded in it work properly using a solution I found elsewhere in Stackoverflow. My new storyboard simply added these attributes to the scroll view, which seemed fine to me.

     delaysContentTouches="NO" canCancelContentTouches="NO"

But in addition, I saw in my storyboard in another scene the following new fragment:

                    <variation key="default">
                        <mask key="subviews">
                            <exclude reference="86H-aM-wei"/>
                        </mask>
                    </variation>

I have no idea where it came from. At first I ignored it because everything seemed to work find on my dev machine. But when the build was built as Release and tested, I got the crash. Removing that spurious(?) fragment fixed the crash and has not seemed to impact anything else.

Conjectural answered 27/7, 2016 at 9:8 Comment(0)
A
0

I had this issue when I had a button in a custom UICollectionViewCell, and had some of its constraints as IBOutlets on that class. I moved the button from the cell to the parent view controller and the IBOutlets were still referenced in the cell but didn't actually exist on the cell so blew everything up. Just needed to remove those IBOutlets and everything worked fine again!

Abolish answered 28/10, 2016 at 9:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.