4

I want to create an animation for my UItextfield. To do this, I created a sub class of UITextField and I stored my code there:

import Foundation  
import UIKit  
class textfieldEdit: UITextField, UITextFieldDelegate {  

    let border = CALayer()  
    let width =  CGFloat(2.0)  

    required init?(coder aDecoder: (NSCoder!)) {  
        super.init(coder: aDecoder)  
        self.delegate=self;  
        border.borderColor = UIColor( red: 216/255, green: 216/255, blue: 216/255, alpha: 100 ).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  
    }  

    override func draw(_ rect: CGRect) {  
        border.frame = CGRect(x: 0, y: self.frame.size.height - width, width:  self.frame.size.width, height: self.frame.size.height)  
    }  

    override func awakeFromNib() {  
        super.awakeFromNib()  
        border.frame = CGRect(x: 0, y: self.frame.size.height - width, width:  self.frame.size.width, height: self.frame.size.height)  
    }  

}  

However, when someone clicks inside the textfield, I want to update the border of the textfield and make it green. Is there an efficient way to do this? I tried to assign a function to whenever someone touches inside of the textbox in my main view controller but it doesnt seem to work.

codeislife
  • 97
  • 2
  • 11

1 Answers1

2

I'd suggest not combining these behaviours. Instead, add an UIView with "Touches Enabled", then add a touch listener to this view, on tap send a message to a convenience method in your custom uitextfield, or just change the looks of it directly.

This also works with an UIButton.

For layout purposes just make the button or view have the same edge constraints as your label as well as its center X and Y position.

Don't forget to call the "setNeedsDisplay" for your custom text label to force it to redraw.

What is the most robust way to force a UIView to redraw?

Edit: If you are only interested in changing the border color when the UITextfield is currently being edited, add a delegate to it and change the appearance accordingly once when it becomes first responder (is being edited) and once when it resigns being first responder (stops being edited):

How to know when UITextView became first responder

Change border here:

 optional func textViewDidBeginEditing(_ textView: UITextView)

https://developer.apple.com/documentation/uikit/uitextviewdelegate/1618610-textviewdidbeginediting

And set it back to normal here:

 optional func textViewDidEndEditing(_ textView: UITextView)

https://developer.apple.com/documentation/uikit/uitextviewdelegate/1618628-textviewdidendediting

Pochi
  • 13,391
  • 3
  • 64
  • 104
  • Is there a better method than touches enabled? I'd rather use while the user is typing in the textbox because I dont want the animation to go away after one touch – codeislife Jun 19 '17 at 02:29
  • Ah, i see what you wanna do now. You should look at "catching" the responder. So when this uitextfield "BECOMES FIRST RESPONDER" that's when you wanna set the new color. Then when it "RESIGN BEING FIRST RESPONDER" you can return it to the old color. This is what you are looking for, but this has nothing to do with touches. https://stackoverflow.com/questions/7022180/how-to-know-when-uitextview-became-first-responder – Pochi Jun 19 '17 at 02:36
  • Just implement the delegate with the 2 methods I added at the end of my description.@codeislife – Pochi Jun 19 '17 at 02:48