@IBDesignable not working in iOS swift 3
Asked Answered
U

6

15

What I did: Deleted derived data, Restarted xcode, Editor->Refresh all views

I am getting Designables build failed in storyboard when I click on Editor->Refresh all views.

enter image description here

Please check the following code. What am I missing? Textfiled is not getting updated in the storyboard when I change the @IBInspectable value from the attributes inspector.

import UIKit
import QuartzCore

@IBDesignable
class BorderedFloatingTF: UITextField {


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

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

    override func textRect(forBounds bounds: CGRect) -> CGRect {
        return bounds.insetBy(dx: 20, dy: 0)
    }

    override func editingRect(forBounds bounds: CGRect) -> CGRect {
        return textRect(forBounds: bounds)
    }
    override func prepareForInterfaceBuilder() {
        super.prepareForInterfaceBuilder()
        setup()
    }

    // properties..

    @IBInspectable var enableTitle : Bool = false
    @IBInspectable var borderColor: UIColor = UIColor.white {
        didSet {
            layer.borderColor = borderColor.cgColor
        }
    }
    @IBInspectable var borderWidth: Int = 1 {
        didSet {
            layer.borderWidth = CGFloat(borderWidth)
        }
    }
    @IBInspectable var placeHolderColor: UIColor = UIColor.white {

        didSet {
            self.attributedPlaceholder = NSAttributedString(string:self.placeholder != nil ? self.placeholder! : "", attributes:[NSForegroundColorAttributeName: placeHolderColor])
        }
    }

    fileprivate func setup() {
        borderStyle = UITextBorderStyle.none
        layer.borderWidth = CGFloat(borderWidth)
        layer.borderColor = borderColor.cgColor
        placeHolderColor = UIColor.white
    }
}
Undecided answered 17/5, 2017 at 11:42 Comment(3)
What is the problem? I am doing it this way for the first time.Undecided
I tested your code, completely and it's working fine in my system, here I've attached snapshots for the same with your code. Restart your xcode and see. There may be memory related issue with Xcode.Cott
The first step is to find the crash log in Interface Builder crash logs and check the stack trace.Heintz
C
7

Try this

![enter image description here

import QuartzCore

@IBDesignable
class BorderedFloatingTF: UITextField {


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

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

    override func textRect(forBounds bounds: CGRect) -> CGRect {
        return bounds.insetBy(dx: 20, dy: 0)
    }

    override func editingRect(forBounds bounds: CGRect) -> CGRect {
        return textRect(forBounds: bounds)
    }


    // properties..

    @IBInspectable var enableTitle : Bool = false
    @IBInspectable var borderColor: UIColor = UIColor.white {
        didSet {
            layer.borderColor = borderColor.cgColor
        }
    }
    @IBInspectable var borderWidth: Int = 1 {
        didSet {
            layer.borderWidth = CGFloat(borderWidth)
        }
    }
    @IBInspectable var placeHolderColor: UIColor = UIColor.white {

        didSet {
            self.attributedPlaceholder = NSAttributedString(string:self.placeholder != nil ? self.placeholder! : "", attributes:[NSForegroundColorAttributeName: placeHolderColor])
        }
    }

    func setup() {
        borderStyle = UITextBorderStyle.none
        layer.borderWidth = CGFloat(borderWidth)
        layer.borderColor = borderColor.cgColor
        placeHolderColor = UIColor.white
    }
}

Your IBDesignables & IBInspectables both are working fine.

Cott answered 17/5, 2017 at 12:7 Comment(1)
Thanks for your effort @krunal. I created a new project and here the code is working fine.Undecided
H
7

I had this problem with a custom class loading from a xib. Finally stumbled on this solution (had to use the correct bundle for the class, rather than Bundle.main):

@IBDesignable
class DataEntryView: UIView {

@IBOutlet var contentView: DataEntryView!

@IBInspectable var hasError : Bool = false

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

override init(frame: CGRect) {
    super.init(frame: frame)
    self.commonInit()
}

private func commonInit() {
    let bundle = Bundle.init(for: type(of: self))
    bundle.loadNibNamed("DataEntryView", owner: self, options: nil)
    addSubview(contentView)
    contentView.translatesAutoresizingMaskIntoConstraints = false
    contentView.topAnchor.constraint(equalTo: self.topAnchor, constant: 0).isActive = true
    contentView.leftAnchor.constraint(equalTo: self.leftAnchor, constant: 0).isActive = true
    contentView.rightAnchor.constraint(equalTo: self.rightAnchor, constant: 0).isActive = true
    contentView.bottomAnchor.constraint(equalTo: self.bottomAnchor, constant: 0).isActive = true
}
}
Harpole answered 1/10, 2017 at 19:22 Comment(1)
You are life saver bro.. 🤦🏻‍♂️ Was trying to find out reason for not rendering so long..Quincyquindecagon
F
7

I had the same problem on a project. I did the following steps to fix the issue.

In the custom IBDesignable view, make sure that you override both methods

required init?(coder aDecoder: NSCoder)
override init(frame: CGRect)

Add following to your Runpath Search Paths in Build Settings

@loader_path/Frameworks
$(CONFIGURATION_BUILD_DIR)

Clean your project, delete derived data.

That should be all fine.

Flutist answered 2/2, 2018 at 14:54 Comment(1)
My custom control never rendered in IB, after adding the init-methods you mentioned it started working. I did NOT add the run script. You're my hero, thanks!Positivism
I
4

In my project i was missing for setting its value Type. Your problem is not about that. But for other people who are having same issue with me, I wanted to explain.

Example: I was writing like below:

@IBInspectable public var rowOrColoumnCount = 0

But It should have been like:

@IBInspectable public var rowOrColoumnCount : Int = 0
Incurious answered 23/7, 2017 at 18:44 Comment(0)
S
0

Check whether it's correct Module specified in "Module" field. It's specified "Template1" now, is it correct? try also to make all @IBInspectable properties as public

Try to leave only this part:

@IBDesignable
class BorderedFloatingTF: UITextField {

// properties..

@IBInspectable var enableTitle : Bool = false
@IBInspectable var borderColor: UIColor = UIColor.white {
    didSet {
        layer.borderColor = borderColor.cgColor
    }
}
@IBInspectable var borderWidth: Int = 1 {
    didSet {
        layer.borderWidth = CGFloat(borderWidth)
    }
}
@IBInspectable var placeHolderColor: UIColor = UIColor.white {

    didSet {
        self.attributedPlaceholder = NSAttributedString(string:self.placeholder != nil ? self.placeholder! : "", attributes:[NSForegroundColorAttributeName: placeHolderColor])
    }
}

}

Siward answered 17/5, 2017 at 11:57 Comment(2)
It didn't help. Still I am getting build failed as mentioned in the question.Undecided
Check whether it's correct Module specified in "Module" field. It's specified "Template1" now, is it correct? try also to make all @ibinspectable properties as publicSiward
D
-1
  1. Change the custom class back to what it was (UITextField?)
  2. Only set the File's Owner in the xib to BorderedFloatingTF
Dempstor answered 17/5, 2017 at 11:47 Comment(3)
I am using storyboard. How does the code works if I don't set the custom class?Undecided
I am assuming you also have a corresponding xib in the Storyboard called BorderedFloatingTF. In that case, you are referring to the class within itself. When you use your BorderedFloatingTF, in for example a viewcontorller. there you would drag a regular textField and set its class to your custom class.Dempstor
If possible, can you share your project? I am not able to visualize what the project structure is like.Dempstor

© 2022 - 2024 — McMap. All rights reserved.