16

如何防止在UITextField隐藏光标/插入符号/放大镜的同时编辑文本,但仍然显示键盘,因为我使用的inputViewUIPickerView/ UIDatePickerView

设置userInteractionEnabledNO不起作用,因为它不再接收任何触摸并且不会显示键盘。

4

6 回答 6

18

子类 UITextField

//Disables caret
- (CGRect)caretRectForPosition:(UITextPosition *)position
{
    return CGRectZero;
}

//Disables magnifying glass
-(void)addGestureRecognizer:(UIGestureRecognizer *)gestureRecognizer
{
    if ([gestureRecognizer isKindOfClass:[UILongPressGestureRecognizer class]])
    {
        gestureRecognizer.enabled = NO;
    }
    [super addGestureRecognizer:gestureRecognizer];
}

在你的 UITextFieldDelegate

//Prevent text from being copied and pasted or edited with bluetooth keyboard.
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    return NO;
}

现在只需根据 UIPickerView/UIDatePicker 的结果以编程方式设置文本。

于 2013-08-09T02:19:40.343 回答
15

在 iOS 7 中隐藏光标要简单得多。仍然需要一些技巧来禁用放大镜

textField.tintColor = [UIColor clearColor];
于 2013-11-06T06:36:16.403 回答
5

我希望这对你有帮助。

设置光标 UIColor -> 空。在 UI 中,它将被隐藏。

[[self.textField valueForKey:@"textInputTraits"] setValue:[UIColor clearColor] forKey:@"insertionPointColor"];
于 2013-08-17T14:31:48.763 回答
2

我发现最好的解决方案是

- (CGRect) caretRectForPosition:(UITextPosition*) position
{
    return CGRectZero;
}

- (NSArray *)selectionRectsForRange:(UITextRange *)range
{
    return nil;
}

- (BOOL)canPerformAction:(SEL)action withSender:(id)sender
{
    if (action == @selector(copy:) || action == @selector(selectAll:) || action == @selector(paste:))
    {
        returnNO;
    }

    return [super canPerformAction:action withSender:sender];
}

http://b2cloud.com.au/tutorial/disabling-the-caret-and-text-entry-in-uitextfields/

于 2014-10-09T13:35:31.993 回答
1

没有UITextField交互,但仍然使用inputView

使用这些方法子类 UITextField:

// Hide the cursor
- (CGRect)caretRectForPosition:(UITextPosition*)position
{
    return CGRectZero;
}

// All touches inside will be ignored
// and intercepted by the superview
- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event
{
    return NO;
}

最后一种方法将单独阻止任何编辑和放大镜,因为您将无法点击UITextField.

例如,如果您在 a 中使用文本字段,这非常UITableViewCell有用,然后可以通过 切换 firstResponder 状态tableView:didSelectRowAtIndexPath:

于 2015-04-25T02:48:53.693 回答
0

要禁用与文本字段的任何交互,除了使其成为第一响应者之外,您只需在文本字段上放置一个相同大小的 UIButton。按钮点击事件的代码可能是这样的:

- (IBAction)btnEditPhoneTapped:(id)sender
{
    if (self.tfClientPhoneNo.isFirstResponder == NO) [self.tfClientPhoneNo becomeFirstResponder];
}
于 2014-05-30T17:33:19.457 回答