I've been trying to solve this problem for a while but I cannot find a solution. The thing is I have a custom UITextField with custom properties, for instance, background color, and I reuse this control in my storyboards. The problem is when I run the app on the simulator or device, there is a little delay at runtime between the default storyboard values and the values I set in my custom control. For example, if I drag a UITextField to my view controller in my storyboard, leave all the properties to their default values and just set my custom control class in the Identity Inspector, when I run the code in either the simulator or my device, I see a display delay between the default white background color for this UITextField and the custom one set in my custom class. Is someone experiencing this issue? Here a snippet of my class:
import UIKit
import SwifterSwift
@IBDesignable
class TextField: UITextField {
// MARK: - Properties
// Left padding for text and placeholder
@IBInspectable var textPadding = Constants.Views.textFieldTextPadding {
didSet {
setNeedsDisplay()
}
}
// MARK: - Property Overrides
// change the default height
override var intrinsicContentSize: CGSize {
var contentSize = super.intrinsicContentSize
contentSize.height = Constants.Views.textFieldHeight
return contentSize
}
// MARK: - Initializers
// IBDesignables require both of these inits, otherwise we'll
// get an error: IBDesignable View Rendering times out.
// http://stackoverflow.com/questions/26772729/ibdesignable-view-rendering-times-out
override init(frame: CGRect) {
super.init(frame: frame)
setupDefaults()
}
// This attribute hides `init(coder:)` from subclasses
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
setupDefaults()
}
// MARK: - Method Overrides
override func prepareForInterfaceBuilder() {
super.prepareForInterfaceBuilder()
setupDefaults()
}
//provides left padding for the text
override func textRect(forBounds bounds: CGRect) -> CGRect {
return super.textRect(forBounds: bounds).insetBy(dx: textPadding, dy: 0)
}
// provides left padding for the editing text
override func editingRect(forBounds bounds: CGRect) -> CGRect {
return textRect(forBounds: bounds)
}
// provides left padding for the placeholder text
override func placeholderRect(forBounds bounds: CGRect) -> CGRect {
return textRect(forBounds: bounds)
}
// MARK: - Private Methods
func setupDefaults() {
// set default font
font = Constants.Views.textFieldFont
// set default colors
textColor = Constants.Views.textFieldTextColor
tintColor = textColor // default for image tinting and cursor color
backgroundColor = Constants.Views.textFieldBackgroundColor
// set border style
borderStyle = UITextBorderStyle.none
// rounded corners first, order matters if setting shadow
cornerRadius = Constants.Views.cornerRadius
// set the shadow
setShadow(color: Constants.Views.shadowColor, radius: Constants.Views.shadowRadius,
offset: Constants.Views.shadowOffset, opacity: Constants.Views.shadowOpacity)
// clear button
clearButtonMode = .whileEditing
// show a Done button in the keyboard to dismiss it
showDoneButton = true
}
}