Draw beautiful diagram effortlessly on ipad. Click here to know more..

Wednesday, March 28, 2012

Making the iOS text field/view visible which is located under keyboard

The text view being hidden by keyboard is a very common issue faced by iPhone/iPad developers. When you touch a text view, the text view becomes first responder and keyboard automatically appears. If the text view is lying somewhere in lower part of the screen, most probably it will get hidden under the keyboard. The solution is to scroll the text view up enough so that it comes above the keyboard. To do that, you need to add the text view in UIScrollView so that it can be scrolled up.
Here is the strategy:
  • Whenever keyboard appear, check if it is hiding the text view or not.
  • If it is hiding the text view, then scroll the text view up so that it becomes visible.
  • When the keyboard disappears, then scroll the text view to its original position.
Here is the sample code (similar code used in Lekh Diagram):

First register for the keyboard appearing/disappearing event:
[[NSNotificationCenter defaultCenter] addObserver:self
            name:UIKeyboardDidShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
            name:UIKeyboardWillHideNotification object:nil];
The handler for the keyboard appearing event:
- (void)keyboardWasShown:(NSNotification*)aNotification {
    NSDictionary* info = [aNotification userInfo];
    CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;

    // get the keyboard height
    double keyboardHeight = kbSize.height;
    UIInterfaceOrientation currentOrientation = [[UIApplication sharedApplication] statusBarOrientation];
    // The calculations are little different for landscape mode.
    if (UIInterfaceOrientationLandscapeLeft == currentOrientation ||UIInterfaceOrientationLandscapeRight == currentOrientation ) {
        keyboardHeight = kbSize.width;
    // save the position of the scroll view, so that we can scroll it to its original position when keyboard disappears.
    originalScrollOffset = scroller.contentOffset;

    CGPoint cp = [editView convertPoint:editView.bounds.origin toView:[UIApplicationsharedApplication].keyWindow.rootViewController.view];
    cp.y += editView.frame.size.height;
    CGRect bounds = [[UIScreen mainScreen] applicationFrame];
    double sh = bounds.size.height;
    UIInterfaceOrientation currentOrientation = [[UIApplication sharedApplication] statusBarOrientation];
    if (UIInterfaceOrientationLandscapeLeft == currentOrientation ||UIInterfaceOrientationLandscapeRight == currentOrientation ) {
        sh = bounds.size.width;

    // scroll if the keyboard is hiding the text view
    if(cp.y > sh - keyboardHeight){
        double sofset = cp.y - (sh - keyboardHeight);
        CGPoint offset = scroller.contentOffset;
        offset.y += sofset;
        scroller.contentOffset = offset;

And when the keyboard disappears:
- (void)keyboardWillBeHidden:(NSNotification*)aNotification
    // restore the scroll position
    scroller.contentOffset = originalScrollOffset;

The above code is used in "Lekh Diagram" ipad app. Lekh Diagrma is a sketch recognition diagramming app.
You can get the Lekh Diagram from app store:

You can see how content in scrolled when the keyboard appears in the demo of Lekh Diagram:

Learn more about Lekh Diagram: http://www.avabodh.com/lekh


  1. Note for googlers: visit the link below

    1. Forming a wonderful art and including a case in your drawing surface with the Mixtiles promo code frame. Drawing your bright environment and showing the skills as an artist. The designer or artist is a unique person that is different from others.

  2. Good post. Speaking about technologies these days. All of the paperwork is left behind and now it is time to go virtual. Read about data room m&a and it will make sense to you.

  3. In my opinion, such researches will help you in the future. You can answer your children when they ask about family, etc. More over, you can help them with doing homework using https://domyhomework.guru/blog/make-homework-fun

  4. This some way or another is turning obliterating as a great part of the street conflicts are happening because of the utilization of TWD. To illuminate this inconvenience, versatile organizations are concocting Audio text message include which is making it advantageous to connect while driving even while texting.program that reads text

  5. Often this was met with slightly blank expressions - and rightly so as not many translation service providers would lay claim to offering poor quality translations. Technical Translation service

  6. Not a lot of businesses are aware of how powerful Bulk Texting is as a communication tool. When compared to emails, calls and other marketing channels, it is far superior due to its direct reach, response rates and cost which are three major check points every business is aiming for.

  7. This is colossally and superfluously tedious. You ought to most likely respite and begin again effectively, anytime in the record, and whenever, effortlessly. text to speech recorder

  8. خدمات تشمل جميع أركان تنظيف المنزل للحمامات والمطابخ تنظيف السجاد والأريكة بالإضافة إلى تنظيف وتطهير البلاط والأرضيات شركة النجوم لخدمات التنظيف
    شركة تنظيف فلل بجدة
    شركة تنظيف بيوت بجدة
    شركة تنظيف شقق بجدة

  9. Sounds interesting enough; maybe I'll watch out for this. with free guilds and quests

  10. https://hmzapc.com/fl-studio-cracked-download-here/
    FL Studio Crack is exclusive software. The software is used for Latest Music Creations. Its previous name is Fruity-loops. It’s a full product atmosphere or Latest Music Creations (LMC). Serving more further than 18 years of latest built. That provides everything.

  11. https://cracksoon.com/mcafee-antivirus-crack-key-full-download/
    Cracked Here is a useful place where you can easily find Activators, Patch, Full version software Free Download, License key, serial key, keygen, Activation Key and Torrents. Get all of these by easily just on a single click.

  12. https://crackpluskey.com/autodesk-3d-max-crack-key-2020-here/
    Autodesk 3ds Max Crack is the modelling and rendering software. Which is design for visualization, games and animations software? The 3d Max offers a flexible and rich toolset for the users that the user creates the premium design for full aristocratic control view. Therefore it creates a massive new world in the games industry. It gives high-quality visualization to architect workers. It models final interiors and all kind of objects which is used in them

  13. https://usecracked.com/adobe-photoshop-cc-full-crack-key-torrent/
    Cracked Here is a useful place where you can easily find Activators, Patch, Full version software Free Download, License key, serial key, keygen, Activation Key and Torrents. Get all of these by easily just on a single click.

  14. A text field is a single-line, fixed-height field, often with rounded corners, that automatically brings up a keyboard when the user taps it in Check us out

  15. When I was still in school, I used the services of my assistants and chose this site https://payforessay.net/, which I often visited for writing work, which got the job done quickly and efficiently.

  16. Dependably my best memories were just about playing in the casino. By and by, paying little psyche to whatever else I like to give bier haus slots different games a shot the web. You can get an enormous stack of money in a casino if you sort out some way to deal with play. In like way, I can help you and give you marvelous tips on the most talented procedure to play conclusively in a casino