禁用 UITextField 键盘?

发布于 2024-11-01 02:16:13 字数 107 浏览 2 评论 0原文

我在应用程序中放置了一个数字键盘,用于将数字输入到文本视图中,但为了输入数字,我必须单击文本视图。一旦我这样做,常规键盘就会出现,这是我不想要的。

如何完全禁用键盘?非常感谢任何帮助。

I put a numeric keypad in my app for inputing numbers into a text view, but in order to input numbers I have to click on the text view. Once I do so, the regular keyboard comes up, which I don't want.

How can I disable the keyboard altogether? Any help is greatly appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(10

且行且努力 2024-11-08 02:16:13

UITextField 的 inputView 属性默认为 nil,这意味着显示标准键盘。

如果您为其分配自定义输入视图,或只是虚拟视图,则键盘将不会出现,但闪烁的光标仍会出现:

UIView* dummyView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 1, 1)];    
myTextField.inputView = dummyView; // Hide keyboard, but show blinking cursor

如果您想隐藏键盘和闪烁的光标,请使用以下方法:

-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
    return NO;  // Hide both keyboard and blinking cursor.
}

The UITextField's inputView property is nil by default, which means the standard keyboard gets displayed.

If you assign it a custom input view, or just a dummy view then the keyboard will not appear, but the blinking cursor will still appear:

UIView* dummyView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 1, 1)];    
myTextField.inputView = dummyView; // Hide keyboard, but show blinking cursor

If you want to hide both the keyboard and the blinking cursor then use this approach:

-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
    return NO;  // Hide both keyboard and blinking cursor.
}
ゃ懵逼小萝莉 2024-11-08 02:16:13

对于 Swift 2.x、3.x、4.x、5.x

textField.inputView = UIView()

可以解决这个问题

For Swift 2.x, 3.x, 4.x, 5.x

textField.inputView = UIView()

does the trick

一梦等七年七年为一梦 2024-11-08 02:16:13

如果它是 UITextField,您可以将其 enabled 属性设置为 NO。

如果它是 UITextView,您可以在其委托中实现 -textViewShouldBeginEditing: 以返回 NO,这样它就永远不会开始编辑。或者您可以将其子类化并重写 -canBecomeFirstResponder 以返回 NO。或者,您可以利用其编辑行为,将数字按钮放入用作文本视图的 inputView 的视图中。这应该会导致在编辑文本视图时显示按钮。这可能是也可能不是您想要的。

If it's a UITextField, you can set it's enabled property to NO.

If it's a UITextView, you can implement -textViewShouldBeginEditing: in its delegate to return NO, so that it'll never start editing. Or you can subclass it and override -canBecomeFirstResponder to return NO. Or you could take advantage of its editing behavior and put your numeric buttons into a view which you use as the text view's inputView. This is supposed to cause the buttons to be displayed when the text view is edited. That may or may not be what you want.

撩发小公举 2024-11-08 02:16:13

根据您现有按钮的工作方式,这可能会破坏它们,但您可以阻止键盘显示,将 textView 的可编辑属性设置为 NO

myTextView.editable = NO

Depending on how you have your existing buttons working this could break them, but you could prevent the keyboard from showing up setting the textView's editable property to NO

myTextView.editable = NO
゛时过境迁 2024-11-08 02:16:13

当同一视图上有两个文本字段时,我遇到了同样的问题。我的目的是为一个文本字段显示默认键盘,然后隐藏第二个文本字段,然后显示一个下拉列表。

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField 

方法根本没有像我预期的那样工作 2 个文本字段,我发现的唯一解决方法是

    UIView* dummyView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 1, 1)];    
    myTextField.inputView = dummyView; 
    myTextField.inputAccessoryView = dummyView; 
    myTextField.tintColor =  myTextField.backgroundColor; //to hide a blinking cursor

这将完全隐藏目标文本字段的键盘(在我的情况下为 DropDownList)并在用户切换到时显示默认键盘第二个文本字段(屏幕截图上的帐号)

在此处输入图像描述

I have the same problem when had 2 textfields on the same view. My purpose was to show a default keyboard for one textfield and hide for second and show instead a dropdown list.

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField 

method simply did not work as I expected for 2 textfields , the only workaround I found was

    UIView* dummyView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 1, 1)];    
    myTextField.inputView = dummyView; 
    myTextField.inputAccessoryView = dummyView; 
    myTextField.tintColor =  myTextField.backgroundColor; //to hide a blinking cursor

This will totally hide the keyboard for a target textField (DropDownList in my case) and show a default one when user switches to the 2nd textfield (Account number on my screenshot)

enter image description here

慈悲佛祖 2024-11-08 02:16:13

输入图片这里的描述有一个简单的技巧。放置一个空按钮
(无文本)位于键盘上方,并为其分配一个操作事件。这将停止键盘出现,您可以在按钮单击的手柄中执行任何您想要的操作

enter image description hereThere is a simple hack to it. Place a empty button
(No Text) above the keyboard and have a action Event assign to it. This will stop keyboard coming up and you can perform any action you want in the handle for the button click

北陌 2024-11-08 02:16:13

要禁用 UITextField 键盘:

  1. 转到 Main.Storyboard
  2. 单击 UITextField 将其选中
  3. 显示 属性检查器
  4. 取消选中用户交互已启用

要禁用 UITextView 键盘:

  1. 转到Main.Storyboard
  2. 单击 UITextView 将其选中
  3. 显示属性检查器
  4. 取消选中 可编辑行为

To disable UITextField keyboard:

  1. Go to Main.Storyboard
  2. Click on the UITextField to select it
  3. Show the Attributes inspector
  4. Uncheck the User Interaction Enabled

To disable UITextView keyboard:

  1. Go to Main.Storyboard
  2. Click on the UITextView to select it
  3. Show the Attributes inspector
  4. Uncheck the Editable Behavior
等风来 2024-11-08 02:16:13

我使用了keyboardWillShow通知和textField.endEditing(true):

lazy var myTextField: UITextField = {
    let textField = UITextField()
    // ....
    return textField
}()

override func viewDidLoad() {
    super.viewDidLoad()

    NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow(_:)), name: UIResponder.keyboardWillShowNotification, object: nil)
}

@objc func keyboardWillShow(_ notification: Notification) {
        
    myTextField.endEditing(true)

    // if using a textView >>> myTextView.endEditing(true) <<<
}

I used the keyboardWillShow Notification and textField.endEditing(true):

lazy var myTextField: UITextField = {
    let textField = UITextField()
    // ....
    return textField
}()

override func viewDidLoad() {
    super.viewDidLoad()

    NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow(_:)), name: UIResponder.keyboardWillShowNotification, object: nil)
}

@objc func keyboardWillShow(_ notification: Notification) {
        
    myTextField.endEditing(true)

    // if using a textView >>> myTextView.endEditing(true) <<<
}
猥琐帝 2024-11-08 02:16:13
private void TxtExpiry_EditingDidBegin(object sender, EventArgs e)
    {
        ((UITextField)sender).ResignFirstResponder();
    }

在 C# 中,这对我有用,我不使用故事板。

private void TxtExpiry_EditingDidBegin(object sender, EventArgs e)
    {
        ((UITextField)sender).ResignFirstResponder();
    }

In C# this worked for me, I don't use the storyboard.

娇纵 2024-11-08 02:16:13

在 Xcode 8.2 中,您可以通过取消选中状态“启用”选项来轻松完成此操作。

  1. 单击您想要不可编辑的文本字段
  2. 转到右侧的 attrube 检查器取消
  3. 选中状态的“启用”

在此处输入图像描述

或者如果您想通过代码来完成此操作。您可以简单地为此文本字段创建一个 @IBOutlet,为其指定一个名称,然后在 viewDidLoad 函数(或任何自定义函数,如果您愿意的话)中使用此变量名称,如下所示(在 swift 3.0.1 中):

override func viewDidLoad() {
    super.viewDidLoad()

    // Do any additional setup after loading the view.

    myTextField.isEditable = false
}

In Xcode 8.2 you can do it easily by unchecking state "enabled" option.

  1. Click on the textField you want to be uneditable
  2. Go to attirube inspector on right side
  3. Uncheck "enabled" for State

enter image description here

Or if you want to do it via code. You can simply create an @IBOutlet of this text field, give it a name and then use this variable name in the viewDidLoad func (or any custom one if you intent to) like this (in swift 3.0.1):

override func viewDidLoad() {
    super.viewDidLoad()

    // Do any additional setup after loading the view.

    myTextField.isEditable = false
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文