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
            selector:@selector(keyboardWasShown:)
            name:UIKeyboardDidShowNotification object:nil];
    
[[NSNotificationCenter defaultCenter] addObserver:self
            selector:@selector(keyboardWillBeHidden:)
            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:
https://itunes.apple.com/us/app/lekh-diagram/id576124115

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

41 comments:

  1. Note for googlers: visit the link below
    http://stackoverflow.com/questions/1126726/how-to-make-a-uitextfield-move-up-when-keyboard-is-present/21096604#21096604

    ReplyDelete
    Replies
    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.

      Delete
  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.

    ReplyDelete
  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

    ReplyDelete
  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

    ReplyDelete
  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

    ReplyDelete
  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.

    ReplyDelete
  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

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

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

    ReplyDelete
  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.

    ReplyDelete
  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.

    ReplyDelete
  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

    ReplyDelete
  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.

    ReplyDelete
  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

    ReplyDelete
  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.

    ReplyDelete
  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

    ReplyDelete
  17. Presently many new companies are opening everywhere on the world, this can straightforwardly be known as the hundred years of new businesses, since all that can go to our brains we can carry out, having, obviously, a decent group of trained professionals and some sort of start-up capital. In each startup, a group of designers is vital, who will completely submerge themselves simultaneously and do all that could be within reach to guarantee that your startup is completely figured it out. Consistently an ever increasing number of organizations show up in any market, in any of their varieties. When beginning a totally new business, recruiting a whole group on the double is very expensive and risky, so you need to search for alternate approaches to communicate with the labor force. Recruiting a backbone js specialist for a recently taught business assumes a vital part later on result, so a positive result can be impacted from the beginning. A solid individual who comprehends everything being developed for a startup is critical, he is liable for the specialized execution. Practically all advanced new companies are computerized, so discovering web designers for them is turning into the fundamental assignment. In the event that you have a decent engineer, you will have an exceptionally brisk beginning, expanded creation, great quality.

    ReplyDelete
  18. Letasoft Sound Booster Crack
    Letasoft Sound Booster Crack is software that will amplify sound volume to a brand new extent. It’s specifically useful for those wherever the system is supplied with larger music system capacities.

    ReplyDelete
  19. Camtasia Studio Crack
    Camtasia Studio Crack is the best tool and facilitates the software for the records the screen and video editing. Therefore this tool is much professional and the best quality of software that has friendly and easy to use.

    ReplyDelete
  20. FL Studio Crack
    FL Studio Crack provides the facility of creating the best and superb music ever. On the other hand, people can use it to produce the best collection of audio sounds.

    ReplyDelete
  21. AnyDesk Premium Crack
    AnyDesk Premium Crack is the most popular and also brand-new software application used to design the fresh-looking interface. Also, this is the most versatile and even the light weighted application.

    ReplyDelete
  22. Virtual DJ Build Crack
    Virtual DJ Build Crack is a powerful software that you can utilize for music and also for video mixing. This software is based on nearly all the abilities. That use of music and video quality enhancing.

    ReplyDelete
  23. Tenorshare 4uKey Crack
    Tenorshare 4uKey Crack software lets to unlock all the locked all devices. Therefore, this tool uses to unlock all the forget pass easily. As you use the digit and secure the pass.

    ReplyDelete
  24. Aster v7 Crack
    Aster v7 Crack could be your stage which transforms your own system to a multiuser channel. It is two or even more users to manually function on the personal computer at the same time. The particular settings lets multiple end-users to make use of a pc for someone stage.

    ReplyDelete
  25. I really like your content. Your post is really informative. I have learned a lot from your article and I’m looking forward to applying it in my article given below!.
    OriginCrack
    vstcrackss.com
    TubeDigger Crack Free Download
    Enscape3D Crack Download
    Unity Pro Crack Download
    Idm Crack Download

    ReplyDelete
  26. I wondered for a long time a nice girl I might chase. This issue was funded by a website. They thought of me immediately as the best service for finding slovenian women, who have changed my life for. Slovenian brides have the worry about women living by the sea, but they are much more interesting in their character and beauty. For those who want your love, I propose you. When you know that my new girlfriend is Slovenia, I don't regret using a bride placement services. I advise someone who wants love to have the internet.

    ReplyDelete
  27. Hi! Thanks for this info. Are you interested in crypto trading? On my site you can watch a review of the best telegram channels with crypto signals free.

    ReplyDelete
  28. 3DMark Crack
    3DMark Crack is a custom software of benchmarking for a Mac system. Produce through the Future Mark company. It also looks at the appearance of 3D graphics processing of a Pc. And also scans it to judge its speed.

    ReplyDelete
  29. 3DMark Crack
    3DMark Crack is a special tool that use to measure the gaming performance of PC. It checks the system and hardware capability to make the overall performance. Also, this tool includes all the tools to measure all the device gamming

    ReplyDelete
  30. Wondershare Dr Fone Crack
    Wondershare Dr Fone Crack is the best program, and it will help the users to regain the lost files and information. Further, it can recover information on your devices without any further issues.

    ReplyDelete
  31. Adobe Acrobat Pro DC Crack
    Adobe Acrobat Pro DC Crack is application software. This software enables us to create a view and manage the PDF file easily. The adobe acrobat reader is easily available on the computer system and the mobile phone also.

    ReplyDelete
  32. CCleaner Pro Crack
    CCleaner Pro Crack the best tool for MS Windows to clean out all kinds of junk files. Therefore, it also uses to clean the computer and make the system more smooth. While it also uses to secure the system to make the data secure.

    ReplyDelete
  33. SimLab Composer Crack
    SimLab Composer Crack is the advanced software for creating professional and photorealistic animations. Users can take advantage of the form export formats in other related CAD software

    ReplyDelete
  34. Golden Software Grapher Keygen
    Golden Software Grapher Keygen is a full-featured scientific graphics package, allowing the user to import data in many formats, create and integrate a variety of 2D and 3D graphic types, and customize graphics with unlimited detail.

    ReplyDelete
  35. https://proscrack.co/angry-birds-epic-crack/
    Angry Birds Epic Serial Key was a success because it wowed gamers with great gameplay and unforgettable characters. For this reason, Rovio Entertainment has taken the right step by using its success to create sequels and spin-offs like the Angry Birds Epic RPG. In this game, the birds prepare for a new adventure as they head to Piggy Islands.

    ReplyDelete
  36. The student doesn't have time as they're working as well as studying, so they can't do the best essay they can themselves. Looking to free up your time homework help cpm? Consider the essay writing service! Let's face it; there are not enough hours in the day to write essays.

    ReplyDelete