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 6032173
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T05:17:41+00:00 2026-05-23T05:17:41+00:00

I have a UIScrollView designed to enable scrolling when the keyboard is up. The

  • 0

I have a UIScrollView designed to enable scrolling when the keyboard is up. The positioning of the scroll view is fine in portrait mode; I have positioned it in Interface Builder, and set the contentSize in my code …

CGSize scrollContentSize = CGSizeMake(360, 221);
self.scrollView.contentSize = scrollContentSize;

However, I am struggling to work out how to change the position of the scrollview so it works effectively when the orientation changes to landscape (and back). Do I somehow need to position it in Interface Builder for landscape(?), and then change the content size in my code dependent on the orientation?

Edit:
Im getting the keyboard size as follows, and changing the content size. This works when i move to Portrait, but the frame is positioned slightly wrong in landscape mode.

 (void)keyboardWillShow:(NSNotification *)n{

    if (keyboardIsShown) {
    return;
    }

    NSDictionary* userInfo = [n userInfo];

    CGRect _keyboardEndFrame;
    [[userInfo valueForKey:UIKeyboardFrameEndUserInfoKey] getValue:&_keyboardEndFrame];
    CGFloat _keyboardHeight;
    UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];
    if (orientation == UIDeviceOrientationPortrait || orientation == UIDeviceOrientationPortraitUpsideDown) {
    _keyboardHeight = _keyboardEndFrame.size.height;
    }
    else {
        _keyboardHeight = _keyboardEndFrame.size.width;
    }

    // resize the noteView
    CGRect viewFrame = self.scrollView.frame;
     viewFrame.size.height -= _keyboardHeight;

    if (UIDeviceOrientationIsPortrait(orientation)){
        CGSize scrollContentSize = CGSizeMake(360, 221);
        self.scrollView.contentSize = scrollContentSize;
    }
    else if (UIDeviceOrientationIsLandscape(orientation)){
        CGSize scrollContentSize = CGSizeMake(520, 275);
        self.scrollView.contentSize = scrollContentSize;
    }

    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationBeginsFromCurrentState:YES];
    [UIView setAnimationDuration:0.3];
    [self.scrollView setFrame:viewFrame];
    [UIView commitAnimations];

    keyboardIsShown = YES;
}
  • 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-05-23T05:17:41+00:00Added an answer on May 23, 2026 at 5:17 am

    What you want to do is figure out the where the top of the keyboard is relative to the view that contains your scrollView. Fortunately, there is a function in UIView called convertRect:fromView: that will take the frame from one coordinate system and map it to another coordinate system. In your case, you know the frame of the keyboard relative to the main window, but would like to know where the top of the keyboard is relative to the view that contains your scrollView. Once you know where the top of the keyboard intersects with your view, you can modify the height of your scrollView to accommodate.

    Below is a snippet of code I use, which I found somewhere online and don’t take credit for. In your view controller you want to be notified of keyboard show and hide events:

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name: UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name: UIKeyboardWillHideNotification object:nil];
    

    Then implement keyboardWillShow: and keyboardWillHide: like this:

    // When the keyboard shows resize self.textView to touch the top of the keyboard.
    -(void)keyboardWillShow:(NSNotification *)_notification {
        NSDictionary  *userInfo = [_notification userInfo];
    
        NSTimeInterval animationDuration;
        UIViewAnimationCurve animationCurve;
        CGRect keyboardEndFrame;
        [[userInfo objectForKey:UIKeyboardAnimationCurveUserInfoKey] getValue:&animationCurve];
        [[userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] getValue:&animationDuration];
        [[userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] getValue:&keyboardEndFrame];
    
            // Here we figure out where the keyboard overlaps with self.view
            // self.view contains self.textView
        CGRect keyboardFrame = [self.view convertRect:keyboardEndFrame fromView:nil];
        CGFloat textViewWidth = self.view.frame.size.width;
    
        [UIView beginAnimations:nil context:nil];
        [UIView setAnimationDuration:animationDuration];
        [UIView setAnimationCurve:animationCurve];
        self.textView.frame = CGRectMake(0, 0, textViewWidth, keyboardFrame.origin.y);
        [UIView commitAnimations];
    }
    
    // When the keyboard hides return self.textView to fullscreen
    -(void)keyboardWillHide:(NSNotification *)_notification {
        NSDictionary  *userInfo = [_notification userInfo];
    
        NSTimeInterval animationDuration;
        UIViewAnimationCurve animationCurve;
        CGRect keyboardEndFrame;
        [[userInfo objectForKey:UIKeyboardAnimationCurveUserInfoKey] getValue:&animationCurve];
        [[userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] getValue:&animationDuration];
        [[userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] getValue:&keyboardEndFrame];
    
        [UIView beginAnimations:nil context:nil];
        [UIView setAnimationDuration:animationDuration];
        [UIView setAnimationCurve:animationCurve];
        self.textView.frame = self.view.frame;
        [UIView commitAnimations];  
    }
    

    The snippet contains some extra stuff to animate the transition in sync with the keyboard animation.

    In this snippet I use self.textView, which you will probably want to change to self.scrollView. What’s great about this code is that it works in all orientations and on the iPad. Take a look at the code and hopefully it’ll be self explanatory.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a UIScrollView which content is designed with Interface Builder. It has a
I have UIScrollView in my view. Problem is that if I slightly scroll it
I have a UIScrollView . This scroll view scrolls my custom GameView . When
I have a UIScrollView on screen. Scroll view contains a view in which are
I have a UIScrollView with 2 pages, and I can scroll horizontally between them.
I have a UIScrollView that houses a gallery of images the user can scroll
I have a UIScrollView in my project. I have a view controller I would
I have a UIScrollView subclass that I am programmatically scrolling using UIView animations. I'd
I have a UIScrollView in my view control but my UIScrollView can't catch the
Situation: In the main View Controller, I have a UIScrollView and within that I

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.