Display keyboard without animation - iphone

Looked intoUIKeyboardAnimationDurationUserInfoKey but I just can't find anywhere how to set it to a custom value.

UIKeyboardAnimationDurationUserInfoKey is a const string identifier for the dictionary key that holds the animation duration, so there is no way to change it easily.
One way to make the keyboard appear without animation is to observe the keyboard notifications and disable animation when it's about to appear and then reenable them. This, of course, disables any other animation as well.
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(willShowKeyboard:)
name:UIKeyboardWillShowNotification
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(didShowKeyboard:)
name:UIKeyboardDidShowNotification
object:nil];
- (void)willShowKeyboard:(NSNotification *)notification {
[UIView setAnimationsEnabled:NO];
}
- (void)didShowKeyboard:(NSNotification *)notification {
[UIView setAnimationsEnabled:YES];
}
and then then the same for UIKeyboardWillHideNotification/UIKeyboardDidHideNotification notifications.

Try
[UIView performWithoutAnimation:^{
[textField becomeFirstResponder];
}];

iOS8-compatible:
Add the appropriate delegate method:
- (void)textFieldDidBeginEditing:(UITextField *)textField {
[UIView setAnimationsEnabled:NO];
}
or
- (void)textViewDidBeginEditing:(UITextView *)textView {
[UIView setAnimationsEnabled:NO];
}
Add the keyboard notification:
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(didShowKeyboard:) name:UIKeyboardDidShowNotification object:nil];
And method:
- (void)didShowKeyboard:(NSNotification *)notification {
[UIView setAnimationsEnabled:YES];
}

I've found the best solution is using UIView.setAnimationsEnabled(_ enabled: Bool).
Swift 3
UIView.setAnimationsEnabled(false)
textField.becomeFirstResponder()
// or textField.resignFirstResponder() if you want to dismiss the keyboard
UIView.setAnimationsEnabled(true)

The answer of #Vadoff works perfect. Here for Swift 3:
override func viewDidLoad() {
super.viewDidLoad()
//...
// Add observer to notificationCenter so that the method didShowKeyboard(_:) is called when the keyboard did show.
NotificationCenter.default.addObserver(self, selector: #selector(type(of: self).didShowKeyboard(_:)), name: NSNotification.Name.UIKeyboardDidShow, object: nil)
// Make textField the first responder.
textField.becomeFirstResponder() // <- Change textField to the name of your textField.
}
func textFieldDidBeginEditing(_ textField: UITextField) {
// Disable animations.
UIView.setAnimationsEnabled(false)
}
func didShowKeyboard(_ notification: Notification) {
// Enable animations.
UIView.setAnimationsEnabled(true)
}

I had to disable Animations in textViewShouldBeginEditing, textFieldDidBeginEditing did not work for me (iOS 8)

It is pretty simple guys. Do not use UIView:SetAnimationEnabled as that will be potentially troublesome. This is how i remove animation when keyboard show.
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[txtFirstName becomeFirstResponder];
}
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
[txtFirstName resignFirstResponder];
}

Related

how to slide screen in iphone app to look the data which we are entering in textfield [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Making the view slide up to make room for the keyboard?
Xcode/iOS5: Move UIView up, when keyboard appears
As we normally enter data in textfield keyboard appears and it hides the data what we are entering in fields so is there any way that screen should slide up so that we can see data which is entered in the fiedl.
try this code.......
-(void)setViewMovedUp:(BOOL)movedUp
{
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.3]; // if you want to slide up the view
CGRect rect = self.view.frame;
if (movedUp)
{
rect.origin.y -= moveKeyboard;
}
else
{
rect.origin.y += moveKeyboard;
}
self.view.frame = rect;
[UIView commitAnimations];
}
-(void)keyboardWillShow
{
// Animate the current view out of the way
if (self.view.frame.origin.y >= 0)
{
[self setViewMovedUp:YES];
}
else if (self.view.frame.origin.y < 0)
{
[self setViewMovedUp:NO];
}
}
-(void)keyboardWillHide
{
if (self.view.frame.origin.y >= 0)
{
[self setViewMovedUp:YES];
}
else if (self.view.frame.origin.y < 0)
{
[self setViewMovedUp:NO];
}
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
// register for keyboard notifications
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(keyboardWillShow)
name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(keyboardWillHide)
name:UIKeyboardWillHideNotification object:nil];
}
- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
// unregister for keyboard notifications while not visible.
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
}
Edit: moveKeyboard is float. Set its value according to your need.
There are notifications (UIKeyboard[Will|Did][Show|Hide]Notification) that tell you the keyboard is about to appear or disappear, and you can use those to trigger the code that moves your views. There are different ways to move the views -- you can move them yourself, adjusting their positions as you like; you can put them all inside a single view so that you only have to move the container; or you can embed them in a scroll view and simply adjust the content offset for the scroll view.
See Apple's document Managing the Keyboard, and in particular the section called Moving Content That is Located Under the Keyboard. There's sample code there, too, and it works quite well.

iPhone: Hide Keyboard on App Did Enter Background or View Did Disappear

I have a UISearchBar which when clicked, shows the keyboard.
However if the user presses the home button while the keyboard is shown and then goes back into the app, the keyboard is still visible.
How can I hide th keyboard when the app closes/enters background?
I've tried the following in viewDidDisappear:
[eventSearchBar resignFirstResponder];
[eventSearchBar endEditing:YES];
I've also tried this in the delegate in appDidEnterBackground:
[self.rootController.navigationController.view endEditing:YES];
None of these worked.
you can do this in appDelegate....
- (void)applicationDidEnterBackground:(UIApplication *)application
{
[self.window endEditing:YES];
}
Swift version of this:
func applicationDidEnterBackground(application: UIApplication) {
window?.endEditing(true)
}
In your view controller, for example in the init method, register for the UIApplicationWillResignActiveNotification:
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(willResignActive:)
name:UIApplicationWillResignActiveNotification
object:nil];
When the app goes into background, make the search display controller inactive. This removes the focus from the search field and hides the keyboard:
- (void)willResignActive:(NSNotification *)note
{
self.searchDisplayController.active = NO;
// Alternatively, if you only want to hide the keyboard:
// [self.searchDisplayController.searchBar resignFirstResponder];
}
And don't forget to remove the observer in the dealloc method:
[[NSNotificationCenter defaultCenter] removeObserver:self
name:UIApplicationWillResignActiveNotification
object:nil];
Solution in swift 3.2 Version
override func viewDidLoad() {
super.viewDidLoad()
NotificationCenter.default.addObserver(self, selector: #selector(hideTextField), name: NSNotification.Name.UIApplicationWillResignActive, object: nil)
}
deinit {
NotificationCenter.default.removeObserver(self)
}
func hideTextField(){
eventSearchBar.endEditing(true)
}
The best way is to put window?.endEditing(true) in applicationWillResignActive on AppDelegate:
func applicationWillResignActive(_ application: UIApplication) {
window?.endEditing(true)
}
I've had all standard methods fail on my sporadically. So far this is the only way I've gotten solid results.
In topmost controller.
- (void)viewDidLoad {
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(willResignActiveNotification:) name:UIApplicationWillResignActiveNotification object:nil];
}
-(void) willResignActiveNotification:(NSNotification*) vNotification {
[[UIApplication sharedApplication] sendAction:#selector(resignFirstResponder) to:nil from:nil forEvent:nil];
[self setEditing:NO];
}
There is an odd case where a text field will no longer respond to resignFirstResponder or endEditing but still has keyboard up.

IPhones- Have the keyboard slide out with the view

We have a view open with keyboard shown , but when the back button clicked , the view slide out from right while the keyboard will slide only when the view disappeared.
if we call resignFirstResponder at viewwilldisappear, the view slide to the right while keyboard slide down at same time.
Is it possible to let the keyboard slide out with the view?
I have tested this and it works in iOS 5.1, however, I don't think this is recommended behaviour.
for (UIWindow *keyboardWindow in [[UIApplication sharedApplication] windows])
if ([[keyboardWindow description] hasPrefix:#"<UITextEffectsWindow"]) {
NSLog(#"%#", [keyboardWindow description]);
[UIWindow beginAnimations:#"fadeKeyboard" context:nil];
keyboardWindow.frame = CGRectMake(keyboardWindow.frame.origin.x + keyboardWindow.frame.size.width, keyboardWindow.frame.origin.y, keyboardWindow.frame.size.width, keyboardWindow.frame.size.height);
[UIWindow commitAnimations];
}
You can also use a notification UIKeyboardWillHideNotification to detect when the keyboard is going to hide, or just use the above code manually.
There is no standard way to do what you want, but...
Basically, keyboard is just a view, presented in it's own UIWindow on top of all your other windows.
So, theoretically, what you need to do is to find keyboard view and move it in desired direction. I think you should use transform property and don't mess up with frame.
Class keyboardClass = NSClassFromString(#"UIPeripheralHostView");
for ( UIWindow *window in [[UIApplication sharedApplication] windows] ) {
for ( UIView *subview in window.subviews ) {
if ( [subview isKindOfClass:keyboardClass] ) {
// that's keyboard
}
}
}
Edited:
If you're talking about UINavigationController and it's default slide animations during push / pop, then, you just need to invoke resignFirstResponder in viewDidDisappear and becomeFirstResponder in viewWillAppear on your text view. That way your keyboard will slide along with your view.
Try sticking resignFirstresponder in the viewDidDisappear method instead.
**Set notificatins and use these methods.....Hope it solve problem:
First of all set your whole view in scrollView**
-(void)keyboardDidHide:(NSNotification *)notif
{
NSTimeInterval duration = 0.4;
[UIView animateWithDuration:duration animations:
^{
scrollView.contentSize=CGSizeMake(320,scrollOriginalFrame.size.height);
}];
keyboardVisible=NO;
}
-(void)keyboardDidShow:(NSNotification *)notif
{
scrollView.contentSize=CGSizeMake(self.view.frame.size.width, scrollOriginalFrame.size.height+235);
NSTimeInterval duration = 0.4;
[UIView animateWithDuration:duration animations:
^{
[scrollView setContentOffset:CGPointMake(0,162) animated:YES];
}];
keyboardVisible=YES;
}
**In viewDidLoad() add this**
//keyboard
scrollOriginalFrame=self.view.frame;
scrollOriginalFrame.size.height-=103;
scrollView.contentSize=scrollOriginalFrame.size;
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(keyboardDidShow:) name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter]addObserver:self selector:#selector(keyboardDidHide:) name:UIKeyboardWillHideNotification object:nil];
keyboardVisible=NO;

how to add done button in keypad

i need to add button done on keypad.
Apple does n't provide such felicity but some of application i found that done ,next,previous buttons.
like this.
how can i add these and how can i give click event to them.
can any one please help me.
1.Define the done button (= return key):
textField.returnKeyType = UIReturnKeyDone;
2.Add the action-listener:
[textField addTarget:self action:#selector(textFieldDoneEditing:) forControlEvents:UIControlEventEditingDidEndOnExit];
3.Define the action-event:
- (IBAction)textFieldDoneEditing:(id)sender {
[sender resignFirstResponder];
}
Have fun!
EDIT:
Here you can find detailed instructions how to add a Toolbar with Next & Previous above UITextField Keyboard:
http://www.randomsequence.com/articles/adding-a-toolbar-with-next-previous-above-uitextfield-keyboard-iphone/
EDIT2:
Now, I have a really great example for you: "This view extends UITextView adding on top of the keyboard associated with this UITextView a toolbar with a « Done » Button"
I check the code and it is a lot of easier than the first example:
http://blog.demay-fr.net/2009/07/cocoa-how-to-add-a-toolbar-with-button-on-top-of-a-uitextview-in-order-to-add-a-dismiss-button/
EDIT3:
Hmmm, no, I doesn't test to code. But I will test it now!
1.Problem: the right initialization. If I add the UITextView in IB, initWithCoder gets called:
- (id)init {
NSLog(#"init");
if (self = [super init]) {
//register a specific method on keyboard appearence
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
}
return self;
}
- (id)initWithCoder:(NSCoder *)decoder {
NSLog(#"initWithCoder");
if (self = [super initWithCoder:decoder]) {
//register a specific method on keyboard appearence
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
}
return self;
}
- (id)initWithFrame:(CGRect)frame {
NSLog(#"initWithFrame");
if (self = [super initWithFrame:frame]) {
//register a specific method on keyboard appearence
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
}
return self;
}
2.Problem: There's no view with the the Prefix "UIKeyboard":
for (UIWindow *keyboardWindow in [[UIApplication sharedApplication] windows]) {
NSLog(#"keyboardWindow = %#", keyboardWindow);
for (UIView *keyboard in [keyboardWindow subviews]) {
NSLog(#"keyboard = %#", keyboard);
if([[keyboard description] hasPrefix:#"<UIKeyboard"] == YES) {
// THERE'S NO VIEW 'UIKeyboard'!!!
}
}
}
The code doesn't work, I'm sorry... I don't know why there's no view "UIKeyboard"... Maybe the first example will help you at this point and you can build your own solution.

textFieldShouldBeginEditing + UIKeyboardWillShowNotification + OS 3.2

I have multiple textfields on a UIView.
I resign for a previous textField in textFieldShouldBeginEditing method, where following sequence of events are performed
UIKeyboardWillHideNotification is received corresponding to that field where the keyboard for the previous field is hidden.
the method textFieldShouldBeginEditing returns a YES and then
UIKeyboardWillShowNotification is received where the keyboard for the current field is displayed.
However, in OS 3.2 even though textFieldShouldBeginEditing returns a YES, UIKeyboardWillShowNotification for the current field is not received.
The logic works for OS < 3.2
Any ideas where I might be doing wrong?
Listed below a part of my code (with only two text fields in xib).
I need to perform a set of operations at keyboardWillShow and keyboardWillHide Look at the difference on running the code in OS 3.2 and OS < 3.2
Can anyone explain the difference in behaviour?
.h
#interface ExampleViewController : UIViewController
{
IBOutlet UITextField *numericTextField;
IBOutlet UITextField *alphaTextField;
UITextField *lastTextField;
int lastCursorPos;
int cursorPosition;
NSMutableArray *textFields;
}
#property (nonatomic, retain) UITextField *lastTextField;
#property (nonatomic, retain) NSMutableArray *textFields;
#end
.m
- (void)viewWillAppear:(BOOL)animated
{
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(keyboardWillShow:)
name:UIKeyboardWillShowNotification object:self.view.window];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(keyboardWillHide:)
name:UIKeyboardWillHideNotification object:self.view.window];
self.view.backgroundColor = [UIColor groupTableViewBackgroundColor];
self.textFields = [[NSMutableArray alloc] initWithCapacity:2];
[self.textFields insertObject:alphaTextField atIndex:0];
[self.textFields insertObject:numericTextField atIndex:1];
cursorPosition = 1;
[numericTextField becomeFirstResponder];
}
-(void)viewWillDisappear:(BOOL)animated
{
[self setEditing:NO animated:YES];
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
}
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
int index;
for(UITextField *aField in self.textFields){
if (textField == aField){
index = [self.textFields indexOfObject:aField];
}
}
if(index>=0 ){
lastCursorPos = cursorPosition;
self.lastTextField = [self.textFields objectAtIndex:lastCursorPos-1];
cursorPosition = index +1;
}
[self.lastTextField resignFirstResponder];
return YES;
}
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField {
return YES;
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
[textField resignFirstResponder];
return YES;
}
- (void)keyboardWillShow:(NSNotification *)notif {
NSLog(#"Inside keyboardWillShow");
}
- (void)keyboardWillHide:(NSNotification *)notif {
NSLog(#"Inside keyboardWillHide");
}
I believe that as of iOS 3.2, UIKeyboardWillHideNotification and UIKeyboardWillShowNotification are no longer fired when switching between two text fields. Basically, the notifications only fire if the keyboard is actually shown or hidden, and since switching from one text field to another doesn't hide the keyboard, the event doesn't fire.
Prior to iOS 3.2 the events used to fire whenever you changed fields. The new way is arguably more correct, but it does make what you are trying to do a bit more challenging.
You might be better off implementing the delegate for the text fields, then you can check for the shouldBeginEditing/didEndEditing events, or alternatively, you could subclass UITextField and override the becomeFirstResponder/resignFirstResponder methods so that you can hook into them and implement your logic when the fields receive and lose focus.
I think you are trying to change the keyboard types when you are on a particular text field. Instead of tracing it the way your doing simply use the two methods,
- (void)textFieldDidBeginEditing:(UITextField *)textField;
- (BOOL)textFieldShouldReturn:(UITextField *)textField;
The first method is called whenever you touch a textfield for editing.
Here you can write you keyboard changing code
EG: If textfield is of type 1
set Keyboard Type to alphanumeric.
Else if textfield is of type 2
set Keyboard Type to numeric only.
Then the second method is called whenever you press the RETURN key on the onscreen keyboard.
Here you can write the [textfield resignFirstResponder] statement for any incoming textfield control.
Hope this helps.. :) cheers!
When the keyboard appears, the method is called by notificationCenter.
If it's not working set the object to nil.
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(keyboardWillShow:)
name:UIKeyboardWillShowNotification object:self.view.window];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(keyboardWillHide:)
name:UIKeyboardWillHideNotification object:self.view.window];