开发者

Setting a UITextField into editing mode programmatically

开发者 https://www.devze.com 2023-01-23 22:42 出处:网络
I have a UITextField that I want to set into editing mode (keyboard on screen and cursor in text field box) programatically.I know开发者_如何学Python that the user will be in editing mode when this vi

I have a UITextField that I want to set into editing mode (keyboard on screen and cursor in text field box) programatically. I know开发者_如何学Python that the user will be in editing mode when this view appears onscreen, so I want to save the user from having to tap the text field.

The "editing" property of a UITextField is read only - so that doesn't work. Is there a way to set the UITextField into editing mode, with a keyboard onscreen, programmatically?


Call becomeFirstResponder on the UITextField.

Related question: How do I show the keyboard by default in UITextView?


You have to call [textField becomeFirstResponder];


Indeed call [textField becomeFirstResponder] in Obj-C or textField.becomeFirstResponder() in Swift.

However, make sure you call this in the viewDidAppear and not in the viewDidLoad to prevent strange behaviour (see: When set UITextField as FirstResponder programmatically, cause some weird actions on text editing).

0

精彩评论

暂无评论...
验证码 换一张
取 消