Ios 使用inputView时,禁止编辑UITextField中的文本并隐藏光标/插入符号/放大镜
当我使用带有Ios 使用inputView时,禁止编辑UITextField中的文本并隐藏光标/插入符号/放大镜,ios,objective-c,uitextfield,inputview,Ios,Objective C,Uitextfield,Inputview,当我使用带有UIPickerView/UIDatePickerView的inputView时,如何防止编辑UITextField中的文本以及隐藏光标/插入符号/放大镜,但仍然显示键盘 将userInteractionEnabled设置为NO不起作用,因为它不再接收任何触摸并且不会显示键盘。子类UITextField //Disables caret - (CGRect)caretRectForPosition:(UITextPosition *)position { return CGR
UIPickerView
/UIDatePickerView
的inputView
时,如何防止编辑UITextField
中的文本以及隐藏光标/插入符号/放大镜,但仍然显示键盘
将
userInteractionEnabled
设置为NO
不起作用,因为它不再接收任何触摸并且不会显示键盘。子类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];
}
在uitextfield中委派
//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的结果以编程方式设置文本。我希望这对您有所帮助 设置光标颜色->空。在UI中,它将被隐藏
[[self.textField valueForKey:@"textInputTraits"] setValue:[UIColor clearColor] forKey:@"insertionPointColor"];
在iOS 7中隐藏光标要简单得多。还需要一些技巧来禁用loupe
textField.tintColor = [UIColor clearColor];
要禁用与textfield的任何交互,除了使其成为第一响应者,您只需在textfield上放置一个大小相同的UIButton。按钮点击事件的代码可能如下所示:
- (IBAction)btnEditPhoneTapped:(id)sender
{
if (self.tfClientPhoneNo.isFirstResponder == NO) [self.tfClientPhoneNo becomeFirstResponder];
}
我发现最好的解决办法是
- (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];
}
要使
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
例如,如果您正在使用
UITableViewCell
中的文本字段,然后可以通过tableView:didSelectRowAtIndexPath:
切换第一响应者状态,这将非常有效。这是一种正式的方式吗?我认为这是对苹果私有API的操纵。我觉得这很可疑!使用Walapu的答案。这是可行的,但您仍然可以单击并键入textField,除非您还根据Walapu的答案实现了textField:ShouldChangeCharactersRange:replacementString:。没有手势识别器(iOS 8):(lldb)po[textField手势识别器]->Nil仍然不知道为什么从故事板设置tintColor不起作用,尽管代码起作用。