简体   繁体   English

Swift 文本框自动光标

[英]Swift textfield automatic cursor

Is it possible to put a cursor automatically in the first of textfields, when opening a screen?打开屏幕时,是否可以将光标自动放在第一个文本字段中? For example, when I open a screen with Email and Password, I want to right after that type my email, without pressing a textfield.例如,当我打开一个带有电子邮件和密码的屏幕时,我想在此之后立即输入我的电子邮件,而不是按文本字段。

If you have a static tableView, just take an IBOutlet from your textFields, and in the method viewWillAppear you can call to yourTextField.becomeFirstResponder()如果你有一个静态 tableView,只需从你的 textFields 中获取一个IBOutlet ,然后在方法viewWillAppear你可以调用yourTextField.becomeFirstResponder()

If you have a dynamic tableView, you have some kind of data source object, you can loop in it, find the index of the first textField, then you have to scroll the tableView to that cell, so it becomes visible, and then you should get a reference to the cell with let cell = tableView.cellForRow(at: IndexPathOfYourCell) as? YourCustomTextCell如果你有一个动态的tableView,你有某种数据源对象,你可以在其中循环,找到第一个textField的索引,然后你必须将tableView滚动到那个单元格,让它变得可见,然后你应该使用let cell = tableView.cellForRow(at: IndexPathOfYourCell) as? YourCustomTextCell对单元格的引用let cell = tableView.cellForRow(at: IndexPathOfYourCell) as? YourCustomTextCell let cell = tableView.cellForRow(at: IndexPathOfYourCell) as? YourCustomTextCell , and then you can do something like cell.textField.becomeFirstResponder() let cell = tableView.cellForRow(at: IndexPathOfYourCell) as? YourCustomTextCell ,然后你可以做一些像cell.textField.becomeFirstResponder()

Chose whichever textfield you want to become first responder:选择您想成为第一响应者的任何文本字段:

override func viewDidLoad(){
  super.viewDidLoad()

  emailTextField.becomeFirstResponder()

}

Create an IBOutlet of TextFiled in ViewController and in viewDidLoad() method call myTextField.becomeFirstResponder()在 ViewController 和viewDidLoad()方法中创建 TextFiled 的IBOutlet调用myTextField.becomeFirstResponder()

override func viewDidLoad() {
    super.viewDidLoad()

    myTextField.becomeFirstResponder()
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 光标的背景阴影在 Swift2 中的 Textfield 上发生变化 - Background shadow of the cursor changes on Textfield in Swift2 在文本字段中输入警报而不是键盘->光标停留在文本字段Swift2中 - Alert instead of keyboard for input in textfield --> cursor stays in textfield Swift2 在Swift Collectionview单元格中填充上一个文本字段后如何将光标自动移动到下一个文本字段 - How to move cursor to next textfield automatically after fill previous textfield in Swift Collectionview cells 删除textField中闪烁的光标 - Delete blinking cursor in a textField 如果用户输入的数据不符合预设条件,我需要迅速将光标重新定位在文本字段中 - In swift I need to reposition the cursor in a textfield if the user enters data that does not conform to preset criterion 搜索栏文本字段光标颜色在 mac Catalyst 中没有改变,它显示黑色 ios swift - Search Bar textfield cursor color not changing in mac Catalyst it's showing black color ios swift 如何在 iOS TexfField 中禁用插入点(键入时文本字段中的光标)? (迅速) - How to disable the insertion point (cursor in textfield when typing) in iOS TexfField? (SWIFT) 在其他文本字段上移动光标/焦点 - Move cursor/focus on other textfield 在文本字段中复制/粘贴光标语言 - Copy/Paste cursor language in textfield 使用 TextField 时输入延迟和光标跳转? - Laggy typing and cursor jumps with TextField?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM