I am using UITextField's method becomeFirstResponder to show the keyboard. This is working in iOS 7. But in iOS 8 this method doesn't show the keyboard.
UITextField *txtAddNew = [[UITextField alloc] initWithFrame:CGRectMake(10,10,240, 21)];
txtAddNew.font = [UIFont fontWithName:@"Helvetica" size:16];
txtAddNew.clearButtonMode = UITextFieldViewModeWhileEditing;
txtAddNew.returnKeyType = UIReturnKeyDone;
txtAddNew.delegate = self;
txtAddNew.autocorrectionType = UITextAutocorrectionTypeNo;
txtAddNew.tag = 15;
// Open keyboard
[txtAddNew becomeFirstResponder];
Is there any way to do it in iOS 8?
Swift 4 and iOS 11
In my case, no mater what I tried, I was not able to set first responder until I tried this.
Hope this helps someone stuck with the same problem.
Swift 3
Here is the swift 3 version of the accepted answer. For me to get it to work I also had to add a delay.
For iOS 9.3, the following worked for me:
So You need to make sure You also implement the textFieldShouldEndEditing delegate.
I think you missed the
addsubview
thetxtAddNew
to theMainview
add this line in your code . i hope it will work
Just in case others have the same issue as me:
I had an
IBOutlet
connected to myUITextField
. Erroneously I was initializing this in myviewDidLoad
method. That is,[[textField alloc] init];
. I removed this and everything worked again.