Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8342607
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T05:49:20+00:00 2026-06-09T05:49:20+00:00

I have a function in my app which moves the view up, whenever the

  • 0

I have a function in my app which moves the view up, whenever the keyboard is displayed. Unfortunately there is a bug; the first time you load the view everything works fine, but if you switch to another view, and then switch back, the view no longer moves 🙁

I added some NSLog statements to my code to try and trace the problem. I am using NSNotification, and that is working fine because the method gets called every time.

Then I thought maybe it was a problem with the coordinates of the view, so I added statements that printed out the origin of the view. They print out the correct origin (the ‘moved’ origin), even though the view definitely didn’t move.

So it seems that the Xcode thinks that it has moved the view, but it hasn’t. Has anyone else encountered this behaviour ?


EDIT: here is some code

Setting up the notifications:

        //register for keyboard notifications
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:self.view.window];

        //if the keyboard is already being shown because someone was entering a comment, and then they switch to a textfield, this will move the view back down.
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UITextFieldTextDidBeginEditingNotification object:self.view.window];

        //hide the keyboard
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:self.view.window];

        //hide the keyboard if we're done with the textview
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UITextViewTextDidEndEditingNotification object:self.view.window];

        keyboardIsShown = FALSE;

        tempDelegate.keyboardIsInitialized = TRUE;

the method to show the keyboard and move the view:

-(void)keyboardWillShow:(NSNotification *)notif{

    NSLog(@"keyboardWillShow");
    NSLog(@"type: %@, keyboardIsShown: %@", sender, keyboardIsShown);

    //double check
    if (keyboardIsShown || !sender) {
        NSLog(@"return");
        return;
    }

    //only adjust screen for comment box (which is a textview)
    if(![sender isEqualToString:@"text field"] && [sender isEqualToString:@"text view"]){

        NSLog(@"if");

        NSDictionary* userInfo = [notif userInfo];

        // get the size of the keyboard
        CGSize keyboardSize = [[userInfo objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;

        [UIView beginAnimations:@"ResizeForKeyboard" context:NULL];
        [UIView setAnimationBeginsFromCurrentState:YES];
        [UIView setAnimationDuration:0.3];

        NSLog(@"regular BEFORE: %@", NSStringFromCGRect(regularView.frame));

        regularView.frame = CGRectMake(0, - keyboardSize.height, CGRectGetWidth(imageView.bounds)*scrollView.zoomScale, CGRectGetHeight(imageView.bounds)*scrollView.zoomScale);

        NSLog(@"regular AFTER: %@", NSStringFromCGRect(regularView.frame));

        [UIView commitAnimations];

        keyboardIsShown = YES;


    }
}

and the method to hide the keyboard and move the view back:

-(void)keyboardWillHide:(NSNotification *)notif{

    NSLog(@"keyboardWillHide");

    if (!keyboardIsShown) {
        NSLog(@"return");
        return;
    }

    [UIView beginAnimations:@"ResizeForKeyboard" context:NULL];
    [UIView setAnimationBeginsFromCurrentState:YES];
    [UIView setAnimationDuration:0.3];

    NSLog(@"regular BEFORE: %@", NSStringFromCGRect(regularView.frame));


    self.regularView.frame = CGRectMake(0, 0, CGRectGetWidth(imageView.bounds)*scrollView.zoomScale, CGRectGetHeight(self.imageView.bounds)*scrollView.zoomScale);
    [UIView commitAnimations];

    NSLog(@"regular AFTER: %@", NSStringFromCGRect(regularView.frame));



    keyboardIsShown = NO;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T05:49:21+00:00Added an answer on June 9, 2026 at 5:49 am

    Are you adding or removing the NSNotification at any point?

    Also, can you post your code for how your moving your view? There maybe some issue there.

    EDIT:

    So from what it seems is that your notifications are being removed at some point and not being added back.

    Move the notification init into your viewDidAppear method and remove them in your viewDidDissappear method. This will ensure that whenever your view appears or disappears notifications are added or removed respectively.

    What you are probably doing is adding them in your viewDidLoad, which this method only gets called when the view is first loaded (so only once). So if you push a new view then pop back, they could get removed and not added back.

    EDIT:

    Here is a chuck of code that I usually use when I need to animate with a keyboard.

    #define kOFFSET_FOR_KEYBOARD 80.0
    
    -(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)textFieldDidBeginEditing:(UITextField *)sender
    {
        if ([sender isEqual:mailTf])
        {
            //move the main view, so that the keyboard does not hide it.
            if  (self.view.frame.origin.y >= 0)
            {
                [self setViewMovedUp:YES];
            }
        }
    }
    
    //method to move the view up/down whenever the keyboard is shown/dismissed
    -(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)
        {
            // 1. move the view's origin up so that the text field that will be hidden come above the keyboard 
            // 2. increase the size of the view so that the area behind the keyboard is covered up.
            rect.origin.y -= kOFFSET_FOR_KEYBOARD;
            rect.size.height += kOFFSET_FOR_KEYBOARD;
        }
        else
        {
            // revert back to the normal state.
            rect.origin.y += kOFFSET_FOR_KEYBOARD;
            rect.size.height -= kOFFSET_FOR_KEYBOARD;
        }
        self.view.frame = rect;
    
        [UIView commitAnimations];
    }
    
    
    - (void)viewWillAppear:(BOOL)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
    {
        // unregister for keyboard notifications while not visible.
        [[NSNotificationCenter defaultCenter] removeObserver:self
                                                 name:UIKeyboardWillShowNotification
                                               object:nil];
    
        [[NSNotificationCenter defaultCenter] removeObserver:self
                                                 name:UIKeyboardWillHideNotification
                                               object:nil];
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an iOS app in which every time the view is loaded, all
I have an app which runs in a new window. There are several forms
My company would dearly like to have an iPhone app which, among other functions,
I recently installed the phpBB app using the one click function (I have a
I have this code App.Model('users').find(function (err, users) { users.forEach(function(user) { console.log(user.username); }); }); //make
In my app I have the following: client.on('test', function(req, fn) { var returnArr =
I have an app with >> 50 calls to $.getJSON(..., function(data) {...} ); At
I want to have a java app running, using a function/method (with as little
I have to create a photo gallery app in iPhone. It should function same
For example suppose I have the following app.get('/', function(req, res) { var ip; if(req.headers['x-forwarded-for']){

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.