i have a UI textfield in a subview.I am adding it to a view .When the keyboard pops-up ,i want to animate the view to which i am adding this subview.I have written all my textfield delegate functions in my subview class only.So if i use Animate textfield function it doesn't move the parent view instead thy subview is animated....please help me
            Asked
            
        
        
            Active
            
        
            Viewed 4,711 times
        
    0
            
            
        - 
                    1Im not sure i have fully grasped what you want here.. but this might help http://stackoverflow.com/questions/1126726/how-to-make-a-uitextfield-move-up-when-keyboard-is-present?rq=1 – Michael M Sep 03 '13 at 10:21
- 
                    1Can you post the code you have used please – Adam Richardson Sep 03 '13 at 10:22
- 
                    can you show some code..so that we could figure out whats the problem... – Prashant Nikam Sep 03 '13 at 10:24
- 
                    sorry i can't add my code @adam.Jus tis simple,am adding a subview which has a text filed in it.When the user clicks the textfield,keyboard appears . At that time i want to move the whole view up and not the view which has the textfield.....thanks in advance – Subathra S Sep 03 '13 at 10:26
- 
                    You have to manually move up the view by subscribing to system notification that fires when the keyboard appears/disappears and changing the frame property of the view by yourself – Vik Sep 03 '13 at 10:28
- 
                    This is not automatic behavior. You have to code it, but Apple provides sample code. Of course, this has been asked many times before: http://stackoverflow.com/questions/1126726/how-to-make-a-uitextfield-move-up-when-keyboard-is-present?rq=1 – Marcus Adams Sep 03 '13 at 13:01
- 
                    Here you can find the best solution ever for use of UiviewCotroller, UITableView and UiScrollView with UITextfields. Here you go: https://github.com/michaeltyson/TPKeyboardAvoiding. – iDevAmit Sep 09 '16 at 03:28
2 Answers
15
            
            
        Try this:
[[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:@selector(keyboardWillShow:)
                                                     name:UIKeyboardWillShowNotification
                                                   object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:@selector(keyboardWillHide:)
                                                     name:UIKeyboardWillHideNotification
                                                   object:nil];
in the viewDidLoad
and then this
- (void)keyboardWillHide:(NSNotification *)aNotification
{
    // the keyboard is hiding reset the table's height
    NSTimeInterval animationDuration =
    [[[aNotification userInfo] objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];
    CGRect frame = self.view.frame;
    frame.origin.y += 160;
    [UIView beginAnimations:@"ResizeForKeyboard" context:nil];
    [UIView setAnimationDuration:animationDuration];
    self.view.frame = frame;
    [UIView commitAnimations];
}
- (void)keyboardWillShow:(NSNotification *)aNotification
{
    // the keyboard is showing so resize the table's height
    NSTimeInterval animationDuration =
    [[[aNotification userInfo] objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];
    CGRect frame = self.view.frame;
    frame.origin.y -= 160;
    [UIView beginAnimations:@"ResizeForKeyboard" context:nil];
    [UIView setAnimationDuration:animationDuration];
    self.view.frame = frame;
    [UIView commitAnimations];
}
in your view controller
Most likely you will have to change the value (160) that I put here based on your specific view
 
    
    
        Vik
        
- 1,897
- 12
- 18
- 
                    
- 
                    1
- 
                    You're welcome. Can you please upvote and accept the answer so that people looking for the same thing can be helped in the future? Thanks – Vik Sep 03 '13 at 11:08
1
            
            
        have you tried textFieldDidBeginEditing i don know whether you tried this or not and whether it is proper way or not. I used this it is working less lines to achieve
-(void)textFieldDidBeginEditing:(UITextField *)textField
{
    self.view.frame=CGRectMake(0, -300, 320, 700);
}
this will move your root view to top so your sub view automatically will move to top text field will not be hide behind keyboard and sorry i don have reputation to comment
 
    
    
        Yohan
        
- 1,108
- 9
- 21
 
    