161

I have a very simple subclass of UITextView that adds the "Placeholder" functionality that you can find native to the Text Field object. Here is my code for the subclass:

import UIKit
import Foundation

@IBDesignable class PlaceholderTextView: UITextView, UITextViewDelegate
{
    @IBInspectable var placeholder: String = "" {
        didSet {
            setPlaceholderText()
        }
    }
    private let placeholderColor: UIColor = UIColor.lightGrayColor()        
    private var textColorCache: UIColor!
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.delegate = self
    }
    
    required init(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        self.delegate = self
    }
    
    func textViewDidBeginEditing(textView: UITextView) {
        if textView.text == placeholder {
            textView.text = ""
            textView.textColor = textColorCache
        }
    }
    
    func textViewDidEndEditing(textView: UITextView) {
        if textView.text == "" && placeholder != "" {
            setPlaceholderText()
        }
    }
    
    func setPlaceholderText() {
        if placeholder != "" {
            if textColorCache == nil { textColorCache = self.textColor }
            self.textColor = placeholderColor
            self.text = placeholder
        }
    }
}

After changing the class for the UITextView object in the Identity Inspector to PlaceholderTextView, I can set the Placeholder property just fine in the Attribute Inspector. The code works great when running the app, but does not display the placeholder text in the interface builder. I also get the following non-blocking errors (I assume this is why it's not rendering at design time):

error: IB Designables: Failed to update auto layout status: Interface Builder Cocoa Touch Tool crashed

error: IB Designables: Failed to render instance of PlaceholderTextView: Rendering the view took longer than 200 ms. Your drawing code may suffer from slow performance.

I'm not able to figure out what is causing these errors. The second error doesn't make any sense, as I'm not even overriding drawRect(). Any ideas?

Community
  • 1
  • 1
Albert Bori
  • 9,832
  • 10
  • 51
  • 78
  • 1
    I was getting this error. The object worked fine in a test project, but not in my main project (in a table). According to https://developer.apple.com/library/mac/recipes/xcode_help-IB_objects_media/Chapters/DebuggingCustomViews.html#//apple_ref/doc/uid/TP40014224-CH42-SW1, I chose Choose Editor > Debug Selected Views. Then I got "Could not debug views" "Ensure your framework has the correct build settings for building for iOS." Nothing came up in Google for this error. – Matt Mar 16 '15 at 20:51

22 Answers22

245

There are crash reports generated when Interface Builder Cocoa Touch Tool crashes. Theses are located in ~/Library/Logs/DiagnosticReports and named IBDesignablesAgentCocoaTouch_*.crash. In my case they contained a useful stack-trace that identified the issue in my code.

martn_st
  • 2,576
  • 1
  • 24
  • 30
Petter
  • 3,031
  • 1
  • 18
  • 12
  • 1
    Thanks! This was super helpful! Where did you find the info about the crash reports? :) – Ben-G Jul 31 '15 at 20:31
  • 57
    In my case, I discovered that `initFrame(frame: CGRect)` must be defined. Do so if you provide your own `init` method(s). – Travis Nov 17 '15 at 07:17
  • This was the best solution. I wasn't implementation initWithFrame on one of the views. – HotFudgeSunday Dec 09 '15 at 17:13
  • 3
    I used the diagnostic reports and like @Travis mentioned, I needed to override the `init(frame: CGRect)` explicitly because I had a custom `ini` method. You might want to look through the crash report for `use of unimplemented initializer 'init(frame:)'` directly under `Application Specific Information` in the report. Thanks guys! Double win in this answer! – Chris Feb 26 '16 at 12:51
  • cd ~/Library/Logs/DiagnosticReports – Joel Teply Aug 16 '16 at 18:59
  • Perfect. Don't forget to restart your Xcode if it still throws the error. – geekay Feb 08 '17 at 13:32
  • 4
    As usual, this log file is also available in the **Console** macOS app, under the **User Diagnostic Reports** group. – Paulo Mattos Apr 10 '17 at 15:56
51

I have had the same issue a couple of times. Both times it started when I was loading an IBDesignable nib onto the storyboard when the nib was not able to fit on the view (ie I had a button off of the UIView but still in the nib). Once I fixed that Xcode still gave me errors so I restarted Xcode until it randomly stopped giving me the error.

I hope this helps.

UPDATE: I just killed all processes named "Interface Builder Cocoa Touch Tool", restarted Xcode and the error went away. Don't know if this will always work or not.

shim
  • 9,289
  • 12
  • 69
  • 108
Dustin Williams
  • 3,912
  • 2
  • 16
  • 19
  • Confession: It randomly stopped giving me the error as well, but a lot has changed since then, and I'm not sure what made the error go away. The two most prominent changes are: 1) We rebuilt the project to use the new swift cocoa pods beta for loading in other swift frameworks. 2) I rebuilt the `PlaceholderTextView` to use a subView label instead of actual text value... It may have been either of those things. – Albert Bori Jan 23 '15 at 02:37
  • Update: The error is back, just as magically as it disappeared. – Albert Bori Jan 27 '15 at 21:19
  • 2
    While I still don't understand why, If you don't overwrite any of the `init` methods, the code posted as part of the question no longer shows the `IB Designables` errors and the placeholder is properly rendered in the Interface Builder. – Willington Vega Feb 25 '15 at 02:42
  • @wvega: Can confirm. That's very weird. And very stupid. – Nico Feb 26 '15 at 01:56
  • 2
    Perhaps this is a bug and we just need to wait for it to be fixed. Perhaps in Xcode 6.3. – Youssef Moawad Mar 01 '15 at 07:34
  • 3
    XCode 7.1.1 and still present. Restarting XCode didn't solve the problem. I had to manually kill all processes called Interface Builder Cocoa Touch Tool, then XCode crashed, restarted it and it started working. – Cristian Pena Nov 11 '15 at 19:24
  • 1
    Important point: Indeed FIRST kill the interface builder processes, then stop XCode - if you do it the other way around the problem persists ... – TheEye Nov 25 '15 at 12:44
  • 2
    XCode 8.1 this error still occurs. Originally caused by not having init override but persisted after init added. Had to kill Interface Builder processes as described THEN restart xcode as @TheEye says – John Fowler Oct 30 '16 at 16:05
  • @JohnFowler unfortunately, the error goes away but comes back after a few minutes working in IB for me. – Can Poyrazoğlu Feb 05 '17 at 23:14
  • 1
    Still present in Xcode 8.2.1, your fix worked, however. Thanks! – LinusGeffarth Mar 01 '17 at 12:57
39

In my case, I was doing the next in the initWithFrame/initWithCoder methods to create the view:

className = NSStringFromClass([self class]);
self.view = [[[NSBundle mainBundle] loadNibNamed:className owner:self options:nil] firstObject];

It looks like I was Not supposed to use the Main Bundle, but instead the bundle of the class. So I replaced that code for the following and it worked:

bundle = [NSBundle bundleForClass:[self class]];
className = NSStringFromClass([self class]);
self.view = [[bundle loadNibNamed:className owner:self options:nil] firstObject];

I thought maybe this might help somebody.

jmoukel
  • 794
  • 6
  • 18
14

You could select your custom view in Interface Builder and then use Editor, Debug Selected Views. It will launch so-called IBDesignableAgentCocoaTouch debug session when all breakpoints (including exception breakpoints) work and you could exactly identify the place your view crashes.

Fyodor Volchyok
  • 5,610
  • 4
  • 28
  • 45
  • Perfect! This found my problem, too – an incorrect font. Which explains why only one of six very similar IBDesignables was failing. – zkarj Jan 07 '18 at 00:57
13

For Xcode 8 - Swift

Adding optional value as default value on @IBInspectable causing issue for me.

This won't work:

@IBInspectable var repeatImage: UIImage = UIImage(named:"myImage")!{
      didSet {
       // configureView
      }
}

This should work:

@IBInspectable var repeatImage: UIImage = RepeatImageView.getDefaultImage() {
    didSet {
        // configureView()
    }
}

class func getDefaultImage() -> UIImage {
    if let defaultImage = UIImage(named: "myImage") {
        return defaultImage
    } else {
        return UIImage()
    }
}
shim
  • 9,289
  • 12
  • 69
  • 108
Mohammad Zaid Pathan
  • 16,304
  • 7
  • 99
  • 130
9

I was experiencing the similar Interface Builder issues rendering designables.

Using the technique suggested in this answer I was able to track down the issue to the use of image literals.

Rendering crash

self.backgroundImage.image =  #imageLiteral(resourceName: "rectangleCenter")

No rendering crash

self.backgroundImage.image =  UIImage(named: "rectangleCenter")
Nick Cross
  • 91
  • 1
  • 5
6

Actually if you have some old user defined attributes (which is not valid for current view) at any view in your storyboard, that may cause your agent to crash.

Besides, sometimes it happens just because of a nasty bug of Xcode. To check it out, when you're at storyboard uncheck Editor > Automatically Refresh Views, then move to another file, clean and restart your project. After you entered storyboard again, you can click Editor > Refresh Views and check automatic one again. That one also solved my problem once.

If both didn't work, then probably you have done something wrong about your IBDesignable view, so choose your crashed views in storyboard and debug by clicking Editor > Debug Views

Yusuf Kamil AK
  • 771
  • 8
  • 17
4

I had the same issue and I solved it by adding the 'use_frameworks!' to my project's Podfile.

Hope it helps you.

shim
  • 9,289
  • 12
  • 69
  • 108
Jimmy Ng
  • 101
  • 7
  • This worked for me when I had this issue with JVFloatLabeledTextField – saswanb Feb 06 '18 at 22:22
  • This was what worked for me, adopting an older Obj-C project that was fairly out-of-date. The IB light up like a Christmas tree after this! Thank you. – drew.. Jun 15 '19 at 18:45
4

This is not the case for this question, but maybe I will help someone else.

I had a similar problem when in my @IBDesignable class I did not implemented both:

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

    // custom setup
}

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

    // custom setup
}
zalogatomek
  • 666
  • 1
  • 9
  • 19
  • You should always implement both init methods. Init with frame is used when you init your view from code and init with aDecoder is used from InterfaceBuilder. – Skodik.o Mar 08 '18 at 07:50
3

In my case, it was a problem with OneSignal. Apparently, they have a bug within the version 2.2.0 and above. Switched to 2.1.6 and everything's great again!

Check out this.

Paul Razvan Berg
  • 16,949
  • 9
  • 76
  • 114
2

In my case it was somehow related to a carthage framework that I was using. I had to add $(PROJECT_DIR)/Carthage/Build/iOS to the Runpath Search Paths build setting

AkademiksQc
  • 677
  • 1
  • 6
  • 20
  • After trying all other posted solutions...this was the only fix that worked for me! Thanks! – Joel Mar 13 '17 at 15:37
1

When i debugged this i found out there are some classes which are modifying UI. Typically marquelabel which is a subclass of UILabel or any other class subclassing UIView and drawing ui at run time and colliding with Autolayout engine. Try giving fixed width or height for these custom views. If it doesn't solve your problem try Following solutions:-

Solution 1:- Uncomment #use_frameworks inside your pod file.

Solution 2:- Try deleting derived data. 1. Close Editor window of your Xcode and quit simulator -> 2. Go to Xcode Preferences -> Locations -> 3. Click small grey arrow showing derived data path -> 4. Select your project -> 5. Delete all the folders inside -> 6. Quit Xcode and reopen

Ashish P
  • 1,463
  • 1
  • 20
  • 29
1

Add it to the bottom of your Podfile and run pod install

# Workaround for Cocoapods issue #7606

    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
Tissa
  • 11
  • 2
1

Add this script at the end of my Podfile and performed pod install again.

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
Haroldo Gondim
  • 7,725
  • 9
  • 43
  • 62
1

A major issue is when you are creating @IBDesignable make sure the cocoapod file isn't included in the UITests or else it will cause this crash.

Modesto Cabrera
  • 519
  • 5
  • 6
0

I find the reason is your xib is not the same size as the design in storyboard. Make sure the xib has the same height and width.

0

I was just missing this line of code platform :ios, '7.0' and problem was solved. Just this line in your pod file and update your pod issue will be resolved.

Shahzaib Maqbool
  • 1,479
  • 16
  • 25
0

For me it was a missing signing certificate, because I never ran the app, so Xcode did not yet create a certificate. Once I ran the app, the IBDesignable rendering worked fine.

Florian Pfisterer
  • 1,205
  • 12
  • 21
0

It's like if you got Code from other Developer and you get this error. Just run

pod install

This worked for me. Hope It helps.

Dhruv Khatri
  • 803
  • 6
  • 15
0

Make sure that you are not directly initialising UIImage or UIFont using assets or fonts added in your project.

I always create a private func setUp() in my @IBDesignable custom UI classes. which is called from init(frame: CGRect), init?(coder aDecoder: NSCoder). So I finally updated the setup() as the following.

private func setUp() {

     //... Doing initial configurations

     // iconImageView.image = UIImage(named: "IconImageName")! // Causing the Crash, use if let OR guard let instead
     if let icon = UIImage(named: "IconImageName") {
          iconImageView.image = icon
          iconImageView.frame.size = icon.size
     }

     // nameLabel.font =  UIFont(name: "Calibri-Light", size: 15.0) // Causing the Crash, use if let OR guard let instead
     if let font = UIFont(name: "Calibri-Light", size: size) {
          nameLabel.font =  font
     } else {
          nameLabel.font = UIFont.systemFont(ofSize: size) 
     }

     // Doing other stuffs
}
Sauvik Dolui
  • 5,520
  • 3
  • 34
  • 44
0

I did everything and it did not work till I restarted MAC. Try restarting MAC. Worked for me.

oto
  • 383
  • 4
  • 18
-1

Just let it to build and run on simulator if you have error in somewhere else in project just comment it out and run designable first to update designable and uncomment the other codes. It works for me.

Manfred Radlwimmer
  • 13,257
  • 13
  • 53
  • 62
Daniel
  • 1