问题
I have a UITextField that is first responder. I want to show keyboard when entering the view but I want to do that the user will not be able to edit it and the cursor will be hidden all time as well.
When you click on a keyboard letter, it will be written in the UITextField, but the user will not be able to edit nothing there, even not to copy.
Thanks!
回答1:
Ok, per my comment, my solution is to have a surrogate UITextField
that has its hidden
property set to YES
. What I do is add that hidden text field to the view, and call becomeFirstResponder
on it. The user has no idea this text field exists. In the delegate callback from the text field, I take the text the user typed in and add it to a UITextView
(though you could add the text to whatever you wanted, like a UITextField
like in your question). I turn off userInteractionEnabled
for the visible text view. This creates the effect you desire.
I created a sample project that I uploaded to Github. (If you aren't familiar with it, just click the zip button to download it, unzip it, and open the .xcodeproj file). https://github.com/MaxGabriel/HiddenTextField
回答2:
I had a UISearchBar
property in my viewController
. And I did it like this:
- (void)viewWillAppear:(BOOL)animated
{
[self.searchBar becomeFirstResponder];
}
This should work the same for a UITextField
.
As for disabling editing, use:
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
return NO;
}
You should have set your viewController to be the delegate of UITextField
.
回答3:
Edited answer: Try this:
1. [txtField becomeFirstResponder];
2. txtField.enabled = NO;
3. when some press on keyboard, then txtField.enabled = YES;
Check this out : http://www.youtube.com/watch?v=cKV5csbueHA
来源:https://stackoverflow.com/questions/13661632/show-uitextfield-keyboard-on-firstresponder-even-when-userinteractionenabled-n