ios 如何在 UITextView 中默认显示键盘?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/259819/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 15:22:08  来源:igfitidea点击:

How do I show the keyboard by default in UITextView?

iosiphonecocoa-touchuitextfield

提问by bpapa

I want to create a view that consists solely of a UITextView. When the view is first shown, by default, I'd like the keyboard to be visible and ready for text entry. This way, the user does not have to touch the UITextViewfirst in order to begin editing.

我想创建一个仅由UITextView. 当视图首次显示时,默认情况下,我希望键盘可见并准备好进行文本输入。这样,用户不必触摸第UITextView一个即可开始编辑。

Is this possible? I see the class has a notification called UITextViewTextDidBeginEditingNotificationbut I'm not sure how to send that, or if that is even the right approach.

这可能吗?我看到班级有一个通知,UITextViewTextDidBeginEditingNotification但我不确定如何发送,或者这是否是正确的方法。

回答by Adam Alexander

to accomplish that just send the becomeFirstResponder message to your UITextField, as follows (assuming you have an outlet called textField, pointing to the field in question):

要完成此操作,只需将 becomeFirstResponder 消息发送到您的 UITextField,如下所示(假设您有一个名为 textField 的出口,指向相关字段):

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [textField becomeFirstResponder];
}

回答by Suragch

In Swift

在斯威夫特

To automatically show the keyboard, to the following:

要自动显示键盘,请执行以下操作:

override func viewDidLoad() {
    super.viewDidLoad()

    // show keyboard
    textView.becomeFirstResponder()
}

Notes

笔记

  • This assumes that the text view is editable.
  • Works for both UITextViewand UITextField
  • To hide the keyboard use textView.resignFirstResponder()
  • 这假设文本视图是可编辑的。
  • 适用于UITextViewUITextField
  • 隐藏键盘使用 textView.resignFirstResponder()

回答by Usman

Following worked fine for me using Swift

以下使用 Swift 对我来说效果很好

override func viewDidAppear(animated: Bool) {
    super.viewDidAppear(animated)

    // Show keyboard by default
    billField.becomeFirstResponder()
}

Key is to use the viewDidAppear function.

关键是使用 viewDidAppear 函数。