I have a c开发者_开发技巧ustom keyboard I want to show when the user taps a UITextField. But at the same time I want to show the cursor in the textfield. If if return a NO for canBecomeFirstResponder, it doesn't show the default keyboard but doesn't show the cursor either.
Can someone please help me out?
Thanks.
The answer to your problem is to create a view for your custom keyboard and then edit the inputView
property of your UITextField and pass it the view for your custom keyboard.
I hope that helps.
override following two methods in UITextFieldDelegate. Note that this approach is valid for both UITextField and UITextView (in which case you override corresponding methods in 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;
}
Seems like what you want is a UITextField with a custom keyboard. Create the class CustomKeyboard : UIView
and add buttons/layout the view. Then for your textfield just set the inputView property to an instance of the class CustomKeyboard textField.inputView = customKeyboard;
. You'll need to set the inputView property to be readwrite as well @property (readwrite, retain) UIView *inputView;
By setting the inputView property, the standard iPhone keyboards will not appear when the textfield becomes first responder.
Register as keyboard notification observer (e.g. in the view controller where you want to hide the keyboard):-
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(hideKeyboard:) name:UIKeyboardWillShowNotification object:nil];
Put in the hideKeyboard: function:-
-(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;
}
}
}
}
(Thanks to luvieere in this post for showing me how to get the keyboard)
I'm not sure of the point, but why not just use a UILabel with the same contents of the text field and decorated to look like your text field with a cursor in it. Swap it out for a UITextField when you want input.
There are 2 solutions to your problem. 1) Setting the alpha of the keyboard to 0 will make the keyboard invisible... which may be all you want. The cursor will appear.
2) UITextField implements the UITextInputTraits Protocol. It will always call the keyboard when it becomes the first responder. You will need to inherit from either it or anther class to change that default behavior.
Good luck.
If you tell us what your trying to accomplish we might be about to suggest a more elegant way of accomplishing it.
Have fun.
I see two solutions - either create custom animation (and stop or start it depending on the first responder status of the text field), or play with inputView property.
Here is a solution for inputView approach:
Set
inputView
property of theUITextField
to empty view and ask it to become first responder. This will effectively hide defaultinputView
(i.e. keyboard), but will continue showing blinking cursor.Add Tap gesture recognizer, and when user taps UITextField, set the inputView property to your custom keyboard, dismiss the keyboard and ask the UITextField to become first responder again.
class BlinkingTextFieldVC: UIViewController { var blinkingTextField: UITextField! override func onViewDidLoad() { setupView() } func setupView() { blinkingTextField = UITextField() blinkingTextField.inputView = UIView() // empty view will be shown as input method 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 // set your custom input view or nil for default keyboard blinkingTextField.becomeFirstResponder() } } }
Why do you even need the cursor ? I think all you need to do, is when ever a user press a key on your own keyboard, you can update the text value of the input.
精彩评论