Uikeyboardboundsuserinfokey Is Deprecated, What to Use Instead

UIKeyboardBoundsUserInfoKey is deprecated, what to use instead?

I played with the previously offered solution but still had issues. Here's what I came up with instead:

    - (void)keyboardWillShow:(NSNotification *)aNotification {
[self moveTextViewForKeyboard:aNotification up:YES];
}

- (void)keyboardWillHide:(NSNotification *)aNotification {
[self moveTextViewForKeyboard:aNotification up:NO];
}

- (void) moveTextViewForKeyboard:(NSNotification*)aNotification up: (BOOL) up{
NSDictionary* userInfo = [aNotification userInfo];

// Get animation info from userInfo
NSTimeInterval animationDuration;
UIViewAnimationCurve animationCurve;

CGRect keyboardEndFrame;

[[userInfo objectForKey:UIKeyboardAnimationCurveUserInfoKey] getValue:&animationCurve];
[[userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] getValue:&animationDuration];

[[userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] getValue:&keyboardEndFrame];

// Animate up or down
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:animationDuration];
[UIView setAnimationCurve:animationCurve];

CGRect newFrame = textView.frame;
CGRect keyboardFrame = [self.view convertRect:keyboardEndFrame toView:nil];

newFrame.origin.y -= keyboardFrame.size.height * (up? 1 : -1);
textView.frame = newFrame;

[UIView commitAnimations];
}

UIKeyboardBoundsUserInfoKey is deprecated

Simple: just don't use those keys.

If you actually read the documentation, it will tell you you should use UIKeyboardFrameBeginUserInfoKey and UIKeyboardFrameEndUserInfoKey.

UIKeyboardBoundsUserInfoKey Deprecated

You can use the UIKeyboardFrameBeginUserInfoKey or UIKeyboardFrameEndUserInfoKey key instead of UIKeyboardBoundsUserInfoKey.

what we can use insted of UIKeyboardBoundsUserInfoKey in iphone os 4.0

Here is one solution that I came up with, along with an alternative recommendation from an Apple developer.

Deprecated constant in iOS

Since the presence of a deprecated constant anywhere on your code would raise a warning (and break the build for us -Werror users), you can use the actual constant value to lookup the dictionary. Thank Apple for usually (always?) using the constant name as it's value.

As for the runtime check, I think you're better testing for the new constant:

&UIKeyboardFrameEndUserInfoKey!=nil

So, this is what I'm actually doing to get the keyboard frame (based on this other answer):

-(void)didShowKeyboard:(NSNotification *)notification {
CGRect keyboardFrame = CGRectZero;

if (&UIKeyboardFrameEndUserInfoKey!=nil) {
// Constant exists, we're >=3.2
[[notification.userInfo valueForKey:UIKeyboardFrameEndUserInfoKey] getValue:&keyboardFrame];
if (UIInterfaceOrientationIsPortrait([[UIDevice currentDevice] orientation])) {
_keyboardHeight = keyboardFrame.size.height;
}
else {
_keyboardHeight = keyboardFrame.size.width;
}
} else {
// Constant has no value. We're <3.2
[[notification.userInfo valueForKey:@"UIKeyboardBoundsUserInfoKey"] getValue: &keyboardFrame];
_keyboardHeight = keyboardFrame.size.height;
}
}

I actually tested this on a 3.0 Device and 4.0 Simulator.

UITextfield to be displayed on top of Keyboard - Beginner

OPTION-1: You can refer to this code which has been accepted as answer in this following link:

How to make a UITextField move up when keyboard is present?

This will surely help you and give you your required solution.

OPTION-2: Also here is an ready made TUTORIAL:

http://www.edumobile.org/iphone/iphone-programming-tutorials/scrollview-example-in-iphone/

I think you would be surely helped by this one

OPTION-3: You can refer to this:

http://www.iphonesampleapps.com/2009/12/adjust-uitextfield-hidden-behind-keyboard-with-uiscrollview/

OPTION-4: Apple Documentation:

https://developer.apple.com/library/ios/#DOCUMENTATION/StringsTextFonts/Conceptual/TextAndWebiPhoneOS/KeyboardManagement/KeyboardManagement.html

Hope this helps you.

What is the reason the UIKeyBoardWIllShowNotification called once?

you should dothis in eachClass like this:

-(void) viewWillAppear: (BOOL)animated
{
[super viewWillAppear:animated];

NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
[nc addObserver:self
selector:@selector(keyboardWasShown:)
name:UIKeyboardWillShowNotification
object:nil];
[nc addObserver:self
selector:@selector(keyboardWasHidden:)
name:UIKeyboardWillHideNotification
object:nil];

}

- (void) viewWillDisappear: (BOOL)animated{

[super viewWillDisappear:animated];

NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
[nc removeObserver:self
name:UIKeyboardWillShowNotification
object:nil];
[nc removeObserver:self
name:UIKeyboardWillHideNotification
object:nil];
}

because the notifications are on the application level not to your class level. So if you have added them in one class and not in all classes, then going to the next class. the notification will still call the the key keyboardWasShown: and the other from the class in which you added the notifications hence your local variables like...
viewMoved = YES;

keyboardShown = YES;

will throw the bad excess exceptions

In your case it is also required to do in all 6 view controllers

Hope this helps.

ipad onscreen keyboard origin

Yes, the height of the keyboard is different depending on what language you use. As far as retrieving the height goes, I would suggest checking out: UIKeyboardBoundsUserInfoKey is deprecated, what to use instead?

Not sure if this question should be considered a duplicate.

UIKeyboardFrameBeginUserInfoKey & UIKeyboardFrameEndUserInfoKey

The start frame is where the keyboard is at the beginning of the animation: offscreen if the keyboard is being shown, or onscreen if the keyboard is being hidden. The end frame is where the keyboard will be at the end of the animation: vice versa. You can use the difference between them to write a single method that responds to both hiding and showing the keyboard.

Be sure also to use UIKeyboardAnimationCurveUserInfoKey and UIKeyboardAnimationDurationUserInfoKey when animating your view changes: that way, your animations and the OS's animations will be in sync.

Another hint: The documentation you linked to states, "The rectangle contained in the UIKeyboardFrameBeginUserInfoKey and UIKeyboardFrameEndUserInfoKey properties of the userInfo dictionary should be used only for the size information it contains. Do not use the origin of the rectangle (which is always {0.0, 0.0}) in rectangle-intersection operations". At least on the iPad on OS 3.2, this is not true. Both rects have the same size, while the origin, which is in screen coordinates, differs between the two.

You may find this question helpful: UIKeyboardBoundsUserInfoKey is deprecated, what to use instead?



Related Topics



Leave a reply



Submit