Quote:
Originally Posted by ghanalupo
I do this to animate the view up and down when the keyboard is required.
In the viewDidLoad method I set up a few observers:
Code:
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
Then the two methods that will be called by the notifications are:
Code:
- (void) keyboardWillShow: (NSNotification*) aNotification;
{
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.3];
CGRect rect = [[self view] frame];
rect.origin.y -= 60;
[[self view] setFrame: rect];
[UIView commitAnimations];
}
- (void) keyboardWillHide: (NSNotification*) aNotification;
{
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.3];
CGRect rect = [[self view] frame];
rect.origin.y += 60;
[[self view] setFrame: rect];
[UIView commitAnimations];
}
So when a textfield or textview are touched the screen scrolls up automatically as the keyboard comes in from bottom.
However I have a problem!
I have a textfield at the top of the screen that does not require the screen to be scrolled up when keyboard appears but below that I have a textview that does need the screen scrolling up.
I cannot figure out how to check in the methods which control triggered the notification and process accordingly?
I have tried passing the name of the objects in the addObserver calls but the no notifications occur!
Is there a way to query which control is current selected on the screen?
|
You probably figured this out long ago in your own way, but what i did was i just made if blocks around the code and duplicated it in each statement. So i have two buttons, the first on i say if [UItextfield isFirstResponder]{}
then else if [UItextfield2 isFirstResponder] {//make it animate up and then down}
My problem with this is that when I click on one textfield, then on the other without clicking done on the keyboard, it will not animate up for the first one, then just animate down when i leave the second textfield. Is there a way to just set the screen back to original? I'm actually going to try and make it so that on the animation which moves things back to normal, i just put in the original perameters of the screen (just came up with it as i was typing). So i will set the y back to what it should be to make the screen sit in its place.
//this is what i put in and it moves the screen back to its original place.
//it is not +=, but just = now
rect.origin.y = 0;