aaa111
aaa111

Reputation: 365

IOS Custom UITextField not showing Placeholder text on device and Storyboard

I am having issue with the placeholder text with my following UITextField custom class. It does not display the placeholder text at all when I run in device. Also sometime it does not shows in Storyboard properly.

import UIKit

class UnderlineTextField : UITextField, UITextFieldDelegate
{
    let border = CALayer()
    let numericKbdToolbar = UIToolbar()

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

        delegate  = self

        createBorder()
    }

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

        delegate = self

        createBorder()
    }

    override func layoutSubviews()
    {
        createBorder()
    }

    func createBorder()
    {
        let width = CGFloat(2.0)

        if isEditing == false
        {
            border.borderColor = UIColor(
                                            red   : 55/255,
                                            green : 78/255,
                                            blue  : 95/255,
                                            alpha : 1.0
                                        ).cgColor
        }

        border.frame = CGRect(
                                x:      0,
                                y:      self.frame.size.height - width,
                                width : self.frame.size.width,
                                height: self.frame.size.height
                             )

        border.borderWidth = width

        self.layer.addSublayer(border)

        self.layer.masksToBounds = true

        self.keyboardType = UIKeyboardType.numberPad

        numericKbdToolbar.barStyle = UIBarStyle.default

        let space      = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.flexibleSpace, target: nil, action: nil)
        let callback   = #selector(UnderlineTextField.finishedEditing)
        let donebutton = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.done, target: self, action: callback)

        numericKbdToolbar.setItems([space, donebutton], animated: false)
        numericKbdToolbar.sizeToFit()

        self.inputAccessoryView = numericKbdToolbar
    }

    func pulseBorderColor()
    {
        border.borderColor = UIColor.white.cgColor
    }

    func normalColor()
    {
        border.borderColor = UIColor(red: 55/255, green: 78/255, blue: 95/255, alpha: 1.0).cgColor
    }

    func textFieldDidBeginEditing(_ textField: UITextField)
    {
        pulseBorderColor()
    }

    func textFieldDidEndEditing(_ textField: UITextField)
    {
        normalColor()
    }

    // MARK: On Finished Editing Function
    @objc func finishedEditing()
    {
        self.resignFirstResponder()
    }
}

I even tried to remove the code for having bottom border and delegate, but still no luck. I am not sure whats wrong. Any help would be highly appreciated.

Thanks

Upvotes: 3

Views: 2462

Answers (2)

staticVoidMan
staticVoidMan

Reputation: 20234

Because you forgot

super.layoutSubviews()

as in

override func layoutSubviews() {
    super.layoutSubviews()
    //...
}

Whenever you override a function you must ensure whether to call it on super or not.
With Apple components, you better call it on super


As for it not showing in storyboard, I think it's an Xcode bug.

EDIT: Xcode 9.3 has fixed the bug which prevented the textField placeholder text from showing up in storyboard.

Upvotes: 8

This is a supposedly reported XCode bug running XCode on High Sierra... Same question here

Xcode won't show my textfield placeholder text in storyboard

Upvotes: 0

Related Questions