Hide the cursor of an UITextField

后端 未结 13 817
甜味超标
甜味超标 2020-12-07 07:51

I am using a UITextField with a UIPickerView for its inputView, so that when the user taps the text field, a picker is summoned for th

相关标签:
13条回答
  • 2020-12-07 08:18

    Simply subclass UITextField and override caretRectForPosition

    - (CGRect)caretRectForPosition:(UITextPosition *)position
    {
        return CGRectZero;
    }
    
    0 讨论(0)
  • 2020-12-07 08:19

    As of iOS 7 you can now just set the tintColor = [UIColor clearColor] on the textField and the caret will disappear.

    0 讨论(0)
  • 2020-12-07 08:25

    Check out the property selectedTextRange of the protocol UITextInput, to which the class UITextField conforms. Few! That's a lesson in object-oriented programing right there.

    Hide Caret

    To hide the caret, nil out the text field's selected text range.

    textField.selectedTextRange = nil; // hides caret
    

    Unhide Caret

    Here are two ways to unhide the caret.

    1. Set the text field's selected text range to the end of the document.

      UITextPosition *end = textField.endOfDocument;
      textField.selectedTextRange = [textField textRangeFromPosition:end
                                                          toPosition:end];
      
    2. To keep the caret in the same spot, first, store the text field's selected text range to an instance variable.

      _textFieldSelectedTextRange = textField.selectedTextRange;
      textField.selectedTextRange = nil; // hides caret
      

      Then, when you want to unhide the caret, simply set the text field's selected text range back to what it was originally:

      textField.selectedTextRange     = _textFieldSelectedTextRange;
      _textFieldLastSelectedTextRange = nil;
      
    0 讨论(0)
  • 2020-12-07 08:27

    I simply subclass UITextField, and override layoutSubviews as follows:

    - (void)layoutSubviews
    {
        [super layoutSubviews];
        for (UIView *v in self.subviews)
        {
            if ([[[v class] description] rangeOfString:@"UITextSelectionView"].location != NSNotFound)
            {
                v.hidden = YES;
            }
        }
    }
    

    It's a dirty hack, and may fail in the future (at which point the cursor will be visible again - your app won't crash), but it works.

    0 讨论(0)
  • 2020-12-07 08:29

    If you want to hide cursor, you can easily use this! It worked for me..

    [[textField valueForKey:@"textInputTraits"] setValue:[UIColor clearColor] forKey:@"insertionPointColor"]
    
    0 讨论(0)
  • 2020-12-07 08:32

    Answer provided by the OP, copied from the question body to help clean up the ever growing tail of unanswered questions.

    I found another solution: subclass UIButton and override these methods

    - (UIView *)inputView {
        return inputView_;
    }
    
    - (void)setInputView:(UIView *)anInputView {
        if (inputView_ != anInputView) {
            [inputView_ release];
            inputView_ = [anInputView retain];
        }
    }
    
    - (BOOL)canBecomeFirstResponder {
        return YES;
    }
    

    Now the button, as a UIResponder, have a similar behavior than UITextField and an implementation pretty straightforward.

    0 讨论(0)
提交回复
热议问题