Is it possible to make @IBDesignable override Interface Builder values?

ibinspectable default value
interface builder custom properties
what can you do to get a custom view to display within interface builder
ibinspectable enum
prepareforinterfacebuilder
ibinspectable supported types
ibinspectable objective-c
ibinspectable not working

For example, I want to subclass UIButton and set it's font to 20.0f by default. I can write something like this:

@IBDesignable

class HCButton: UIButton {
  required init?(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)
    self.customInit()
  }

  func customInit () {
    titleLabel?.font = UIFont.systemFontOfSize(20)
  }
} 

But this does not affect preview in Interface Builder, all custom buttons appear with 15.0f font size by default. Any thoughts?

I have created new IBInspectable as testFonts :

import UIKit

@IBDesignable

class CustomButton: UIButton {
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.customInit()
    }

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

    func customInit () {
        titleLabel?.font = UIFont.systemFontOfSize(20)
    }

    convenience init() {
        self.init(frame:CGRectZero)
        self.customInit()
    }

    override func awakeFromNib() {
        super.awakeFromNib()
        self.customInit()
    }

    override func prepareForInterfaceBuilder() {
        super.prepareForInterfaceBuilder()
        self.customInit()
    }
}

Hope it helps you :)

This is working for me.

How to use IBInspectable to adjust values in Interface Builder, When your app is run, the values that were set in Interface Builder are automatically @IBInspectable var endColor: UIColor = UIColor.white override class var Start your free trial now and get 3 months off exclusively for the  A side note: IBDesignable and IBInspectable really don’t have anything to do with each other. With IBInspectable, you can expose a limited amount of property types to Interface Builder or a Storyboard (things like NSNumber, Bool, UIColor, etc). IBDesignable however is used to live render your views in a nib or storyboard. They’re very handy

I think you must override the init with frame initializer as well to affect that

override init(frame: CGRect) {

    super.init(frame: frame)
    self.customInit()
}

Integrate a Custom Control with Interface Builder, Make a custom class Interface Builder compliant using IBDesignable and IBInspectable. That means enums, structs, arrays, and dictionaries, are not allowed. Now apply the @IBInspectable keyword and override didSet for the created variables in the Attributes Inspector and adjust their values. Working with Constraints in Interface Builder. There are three main options for setting up Auto Layout constraints in Interface Builder: You can control-drag between views, you can use the Pin and Align tools, and you can let Interface Builder set up the constraints for you and then edit or modify the results.

EASIER: The following solution usually works or me

import UIKit

@IBDesignable
class CustomButton: UIButton {
    open override func layoutSubviews() {
        super.layoutSubviews()
        customInit()
    }

    func customInit () {
        titleLabel?.font = UIFont.systemFontOfSize(20)
    }
}

I hope that's what you're looking for.

IBDesignable and IBInspectable in Interface Builder, First, as usual, create a brand new Xcode project. Quite uncomfortable from the source code (but still possible) – probably you are going to need I set them up with initial, neutral values. override func layoutSubviews() {. In this case the id field has two String values during building of the object, and the object knows how to construct the aggregated id. Configuring what happens in the building step allows the separation of the builder interface and the value interface.

@IBDesignable and @IBInspectable in Swift 3 ., IBDesignable and IBInspectable , a way to create custom elements and the attributes . This can be directly added to the iOS Interface Builder. Unfortunately, there's no way to assign a CGColorRef type in Interface Builder. However, this is possible through a proxy property. See Peter DeWeese's answer to a different question for a possible solution to this problem. His answer defines a category that allows a proxy color to be set through Interface Builder.

iOS Dev Course: How to create custom @IBDesignable UIView, How to create custom UIView with IB configuration support but without massive code? Yep! custom segment view configured in Interface Builder There are have an default value, but you can change it later in Interface builder. Initializers — we override initializers to configure our class components. Therefore we can’t even override private methods in a subclass.(See this for details). The overriding method must have same return type (or subtype) : From Java 5.0 onwards it is possible to have different return type for a overriding method in child class, but child’s return type should be sub-type of parent’s return type.

Using @IBDesignable and @IBInspectable, Design custom controls in Interface Builder using @IBDesignable and @​IBInspectable. In the following example we will create a custom UIButton and change its override init(frame: CGRect) { super.init(frame: frame) } required Interface Builder in real time and also to be able to change its values from  “The compiler automatically adds some special methods when it creates an enum. For example, they have a static values method that returns an array containing all of the values of the enum in the order they are declared. This method is commonly used in combination with the for-each construct to iterate over the values of an enum type.”

Comments
  • Will @IBInspectable var testFonts: UIFont appear in Interface Builder?
  • @orkenstein No :( It's not displayed. but the font size is changed.
  • Remove it from listing and paste titleLabel?.font = UIFont.systemFontOfSize(20) from original question. It might confuse some wanderer. And thanks for the clue!
  • Yes it's ok, The only thing i can dream about now - make this default value replace Font value in interface builder.
  • Hey, @AshishKakkad, great post! But I notice that awakeFromNib function is not needed in my testing to make the customInit setting visible in the interface builder. Is there a reason why you are adding that? Also, I believe convenience init() is not needed either.