iPhone - 是否可以不显示键盘但仍显示UITextField中的光标?

5

当用户点击UITextField时,我想显示自定义键盘。但同时我想在文本字段中显示光标。如果我返回NO给canBecomeFirstResponder,则不会显示默认键盘,但也不会显示光标。

有人可以帮帮我吗?

谢谢。


如果没有键盘,UITextField中的光标有什么用处呢?其实并不太多,这也可能是为什么它不是一个被支持的特性。 - psychotik
有一个自定义键盘,所以我不想让默认键盘出现,但仍然希望光标出现,以便用户知道他正在输入文本的位置。 - Swastik
2
这肯定有用。Sherry,你找到答案了吗? - Ben Williams
检查这篇帖子对你是否有用的信息.. http://www.iphonedevsdk.com/forum/iphone-sdk-development/2549-prevent-keyboard-popup.html - EmptyStack
8个回答

6
你的问题的答案是创建一个自定义键盘的视图,然后编辑 UITextFieldinputView 属性,并将其传递给你的自定义键盘的视图。
希望这可以帮助到你。

5

请在UITextFieldDelegate中覆盖以下两种方法。请注意,这种方法适用于UITextField和UITextView(在这种情况下,您需要覆盖UITextViewDelegate中相应的方法)。

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
    if (!textField.inputView) {
        //hides the keyboard, but still shows the cursor to allow user to view entire text, even if it exceeds the bounds of the textfield
        textField.inputView = [[UIView alloc] initWithFrame:CGRectZero];
    }
    return YES;
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    return NO;
}

4
似乎你需要一个带有自定义键盘的UITextField。创建类CustomKeyboard:UIView并添加按钮/布局视图。然后,对于你的textfield,只需将inputView属性设置为CustomKeyboard类的实例textField.inputView = customKeyboard;。你还需要将inputView属性设置为可读写@property(readwrite,retain) UIView * inputView;。通过设置inputView属性,当textfield成为第一响应者时,标准的iPhone键盘将不会出现。

1
如果您不需要自定义键盘功能(可能您的文本输入非常不规范),那么创建一个虚拟的UIView(不指定框架)并将其作为.inputView将可以正常工作。然后只需在textField上调用becomeFirstResponder即可。 - HenryRootTwo

2
在您想要隐藏键盘的视图控制器中,注册为键盘通知观察者(例如:):
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(hideKeyboard:) name:UIKeyboardWillShowNotification object:nil];

在代码中添加 hideKeyboard 函数:
-(void)hideKeyboard:(NSNotification *)notification {

    for (UIWindow *keyboardWindow in [[UIApplication sharedApplication] windows]) {
        for (UIView *keyboard in [keyboardWindow subviews]) {
            if([[keyboard description] hasPrefix:@"<UIKeyboard"] == YES) {

                keyboard.alpha = 0;
            }
        }
    }
}

“感谢luvieere在this post中向我展示如何获取键盘。”

在iOS4中对我不起作用。该方法被调用,但没有视图具有UIKeyboard前缀。 - Ben Williams
谢谢,很开心。这真的很有帮助。我想在我的文本字段中显示闪烁的光标,而不显示键盘,它的效果非常棒... - Nirav Patel

0

我不确定你的意思,但为什么不使用一个UILabel,其内容与文本字段相同,并装饰成带有光标的文本字段外观。当需要输入时,将其替换为UITextField。


我不想要默认键盘出现。而且,我如何在UILabel中显示光标? - Swastik
啊,你的意思是说你的自定义键盘并不是真正的键盘,只是一组按钮,所以你想要模拟整个过程? - Andiih

0

你的问题有两个解决方案。 1)将键盘的alpha设置为0将使键盘不可见...这可能是你想要的全部。光标将出现。

2)UITextField实现了UITextInputTraits协议。当它成为第一响应者时,它将始终调用键盘。您需要从它或另一个类继承来更改该默认行为。

祝你好运。

如果你告诉我们你想要实现什么,我们可能会建议更优雅的方法来实现它。

玩得开心。


0

我看到两种解决方案 - 要么创建自定义动画(并根据文本字段的第一响应者状态停止或启动它),要么使用inputView属性进行操作。

这里是一种inputView方法的解决方案:

  1. UITextFieldinputView属性设置为空视图,并要求它成为第一响应者。这将有效地隐藏默认的inputView(即键盘),但仍会显示闪烁的光标。

  2. 添加Tap手势识别器,当用户点击UITextField时,将inputView属性设置为您的自定义键盘,关闭键盘并再次要求UITextField成为第一响应者。

    class BlinkingTextFieldVC: UIViewController {
    
    var blinkingTextField: UITextField!
    
    override func onViewDidLoad() {
        setupView()
    }
    
    func setupView() {
        blinkingTextField = UITextField()
    
        blinkingTextField.inputView = UIView() // 空视图将显示为输入法
        blinkingTextField.becomeFirstResponder()
    
        let tapGuesture = UITapGestureRecognizer(target: self, action: #selector(blinkingTextFieldTapped(_:)))
        blinkingTextField.addGestureRecognizer(tapGuesture)
    }
    
    func blinkingTextFieldTapped(_ gesture: UITapGestureRecognizer) {
        if gesture.state == .ended {
            view.endEditing(true)
            blinkingTextField.inputView = nil // 设置您的自定义输入视图或默认键盘的nil
            blinkingTextField.becomeFirstResponder()
        }
    }
    
    }
    

-1

你为什么需要光标呢? 我认为你只需要在用户按下键盘上的任意键时,更新输入框的文本值即可。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接