I have put a label in a text box and I want to shift this label to left side after tapping this text box. In the following screenshot 'hello' is a label and I want to shift it left after tapping on the text box.
I wrote the following code but the 'textFieldDidBeginEditing' function is not called
@IBOutlet weak var lblhello: UILabel!
func textFieldDidBeginEditing(numtxt: UITextField!) { //delegate method
lblhello.frame = CGRectMake(98, 156, 42, 21)
}
I'm making the assumption based on previous comments that you want to manually specify the frame size, and aren't using constraints.
First off, you need to add
UITextFieldDelegate
to your viewController.This is done like so in the beginning of your VC class:
Then you want to specify the delegate of your
UITextField
, (most likely inviewDidLoad
)Now you can use the textFieldDelegate functions. The on you're looking for is probably this:
If everything is set up correctly that should change the frame of
lblhello
when a user begins editing yourUITextField
.One common problem I often hear for people saying this doesn't work. Is caused by the label being specified in
viewDidLoad()
. Most of the time you want to declare the label beforeviewDidLoad()
, otherwise functions like this doesn't have access to it.In case you want to move it back to it's original position afterwards. You do almost the same as you did in
textFieldDidBeginEditing
except you usetextFieldDidEndEditing
instead.However, on a side-note. I do suggest getting used to using constraints rather than setting the frame for things like this.