Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

@IBDesignable not working in iOS swift 3

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
    }
}
like image 372
abhimuralidharan Avatar asked May 17 '17 11:05

abhimuralidharan


4 Answers

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.

like image 55
Krunal Avatar answered Oct 01 '22 05:10

Krunal


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
}
}
like image 40
Anthony Scott Avatar answered Oct 01 '22 06:10

Anthony Scott


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.

like image 38
Ilker Baltaci Avatar answered Oct 01 '22 06:10

Ilker Baltaci


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
like image 27
Ahmet Kazim Günay Avatar answered Oct 01 '22 06:10

Ahmet Kazim Günay