Anatoliy Gatt Anatoliy Gatt - 1 year ago 106
iOS Question

Move UIView up when the keyboard appears in iOS

I have a UIView, it is not inside UIScrollView. I would like to move up my View when the keyboard appears. Before I tried to use this solution: How to make a UITextField move up when keyboard is present.
It was working fine. But after inserting the data in the textfields, it takes me to another view, and, when I get back to this page, it just jumping, and I couldn't see my text fields. Is there any better solution, for this problem?

Thanks in advance!

Answer Source

Use following code for keyBoard shown and hide

//Declare a delegate, assign your textField to the delegate and then include these methods

-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidShow:) name:UIKeyboardDidShowNotification object:nil];
    return YES;

- (BOOL)textFieldShouldEndEditing:(UITextField *)textField {
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidHide:) name:UIKeyboardDidHideNotification object:nil];

    [self.view endEditing:YES];
    return YES;

- (void)keyboardDidShow:(NSNotification *)notification
    // Assign new frame to your view 
    [self.view setFrame:CGRectMake(0,-110,320,460)]; //here taken -110 for example i.e. your view will be scrolled to -110. change its value according to your requirement.


-(void)keyboardDidHide:(NSNotification *)notification
    [self.view setFrame:CGRectMake(0,0,320,460)];
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download