Tutorial details

How to scroll an obscured textfield above the keyboard | App Code for Sale | Preview

How to scroll an obscured textfield above the keyboard | iOS Tutorial

how to scroll a textfield to a visible area of the view when it’s obscured by the keyboard and how to put it back into its original position, after the keyboard vanishes.

Overview PAGE TOP

Hello and welcome back!

For a long time I wanted to write about a specific topic that surely every programmer encounters in his/her programming life. A topic not that difficult, but an important one. That is, how to scroll a textfield to a visible area of the view when it’s obscured by the keyboard and how to put it back into its original position, after the keyboard vanishes.

Of course, to accomplish something like that, it’s necessary to know when the keyboard is about to appear and when it’s about to disappear. Therefore, in this post I’m going to work with two subjects evenly important, combined in the package of one.

Let’s discuss about it for a while, beginning from the later one. Every time that the keyboard is going to be shown or to be closed, the system (iOS) sends various notifications to the app regarding each state of the keyboard. For example, such notifications are:

  • UIKeyboardDidShowNotification
  • UIKeyboardDidHideNotification
  • UIKeyboardWillShowNotification
  • UIKeyboardWillHideNotification

In this example I chose to observe the UIKeyboardDidShowNotification notification to let my app know when the keyboard is shown and the UIKeyboardWillHideNotification notification for the keyboard closing.

To make the notifications known inside the app, it’s necessary to set up observers and to specify what kind of notifications exactly we want to observe.

I strongly recommend you to take a look in the Apple Documentation about keyboard notifications. That reading will help you understand everything you need to know. Here are some useful links:

  • About NSNotifications
  • Managing the Keyboard

In a couple of words only, I should explain this: When we set up observers for the previously mentioned two notifications, we must also set a custom method for each notification that is going to be called when a notification arrives. That’s it and nothing more. The rest of the work takes place inside these methods. As you’ll find out by yourself, setting observers for notifications is really a matter only of a single line.

Now that we have an idea about how to know when the keyboard is shown or is hidden, it’s time to move on to the next subject. How can we manage to show a textfield into a visible area of the view, if the textfield is obscured by the keyboard? Well, that’s relatively easy too. The main idea consists of placing a scroll view on our view and adding the textfield on the scroll view. Every time the keyboard is shown, if the textfield is not visible, we make the scroll view to scroll until the textfield becomes visible. When the keyboard is closing, we set the scroll view back into its original position. Keep in mind that everything we do must work in both portrait and landscape orientation, so we have to develop our code in a way that serves every orientation.

Enough talking. Let’s go to implementation. The example project was developed in XCode 4.3 and it was tested in Simulator for the iOS 5.1.

STEP 1 – Create the project PAGE TOP

As usual, we first create the example project. Launch the Xcode and select the Single View Application as the template for the project.

project_kind.png

Next, provide a name for the project. I named it UIKeyboardNotifTestApp, even though we’ll do more than observing notifications.

project_name.png

Finally, pick a place to save and go.

STEP 2 – Setup the interface PAGE TOP

Our interface is going to be really simple. Inside the view, we’re going to have only two elements. The scroll view and the textfield.

Go to the ViewController.xib and add a scroll view and a textfield inside the view. Make sure to place the textview somewhere in the bottom of the view, so it’ll be obscured by the keyboard. Note that I’m using the default view created in the Xcode, meaning a 320×460 view (the status bar exists). The view size we’re working on is important, as we’re going to “play” a little with the orientation. Take a look in my Interface Builder window:

ib_window.png

If you want to place the textfield in the same position, here it’s frame:

textfield_frame.png

You can play around a little by changing the color to the textfield (unless you want to have everything white) and adding some text into the placeholder field.

STEP 3 – Outlets and connections PAGE TOP

At this point we need to create two outlets, one for the scroll view and one for the textfield. Since Xcode 4, I use the drag and drop method from the Interface Builder to the .h file. Using this way the connections of the outlets to the views, synthesizations, outlet releases, etc, automatically get written. This is what I mean:

connection1.png

Specify a name for the outlet:

outlet_name.png

If you don’t use the method above, then you have to write everything manually. I don’t go into details, but make sure after you create the outlets to synthesize, release and connect them to the equivalent views. I named the scroll view “scroller” and the textfield “txtDemo”.

Before we move on, your ViewController.h file should look like this:

1   #import <UIKit/UIKit.h>
2   
3   @interface ViewController : UIViewController ,
4   @property (retain, nonatomic) IBOutlet UIScrollView *scroller;
5   @property (retain, nonatomic) IBOutlet UITextField *txtDemo;
6   
7   @end

As you can see, we also need to add the UIScrollViewDelegate and the UITextFieldDelegate to have access to the delegate methods of the scroll view and the textfield.

STEP 4 – Let’s write some code PAGE TOP

At the top of the ViewController.m file, inside the private interface, we’ll declare two private methods, one for each notification that we are going to observe. Also, we’ll declare a private float variable, in which we are going to store the initial content offset (the original place in other words) of the scroll view, so we can put it in its original position when the keyboard goes away. But that’s for later. For now, here is how you should make your private interface inside the .m file and before the @implementation section:

1   @interface ViewController ()
2   
3   @property (nonatomic) float originalScrollerOffsetY;
4   
5   -(void)keyboardWasShown:(NSNotification *)notification;
6   -(void)keyboardWillHide:(NSNotification *)notification;
7   
8   @end

Here we have two methods: The keyboardWasShown and the keyboardWillHide. These are custom methods and you can name them whatever you like. As you can see, we make the methods to accept just one parameter, the notification that the iOS will send. We’ll implement them later.

Let’s go inside the viewDidLoad method. There is some preparation here. At first, we need to setup the scroll view:

1   // Set up the scroll view.
2   scroller.contentSize = CGSizeMake(320.0, 600.0);
3   scroller.delegate = self;

Initially we are in portrait mode, so the width we set in the content size is 320.0 and the height as much as you want. I set it to 600.0.

We also need to set the textfield delegate:

1   // Set the txtDemo textfield delegate.
2   txtDemo.delegate = self;

If you have named the textfield somehow else, put the name you gave instead of the “txtDemo”.

The first important task now. It’s time to set the observers for the two notifications we want to receive from the iOS:

1   // Add two notifications for the keyboard. One when the keyboard is shown and one when it's about to hide.
2   [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWasShown:) name:UIKeyboardDidShowNotification object:nil];
3   
4   [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];

There is not much to explain. In the first case, we set the keyboardWasShown as the custom method that will handle the UIKeyboardDidShowNotification notification when it arrives. In the second case, we set the keyboardWillHide method as the selector for the UIKeyboardWillHideNotification notification.

So, after all the above code, your viewDidLoad method should look like this:

1   - (void)viewDidLoad
2   {
3       [super viewDidLoad];
4       // Do any additional setup after loading the view, typically from a nib.
5   
6       // Set up the scroll view.
7       scroller.contentSize = CGSizeMake(320.0, 600.0);
8       scroller.delegate = self;
9   
10      // Set the txtDemo textfield delegate.
11      txtDemo.delegate = self;
12  
13      // Add two notifications for the keyboard. One when the keyboard is shown and
14      // one when it's about to hide.
15      [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWasShown:) name:UIKeyboardDidShowNotification object:nil];
16  
17      [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
18  }

Let’s implement the two methods now, beginning from the keyboardWasShown.

Begin implementing the keyboardWasShown method and add a NSLog command:

1   -(void)keyboardWasShown:(NSNotification *)notification{
2       NSLog(@"Keyboard is shown.");

Before going into deeper waters, it’s good to store in two float variables the width and the height of our main view. Why? Because if we rotate in landscape mode the Simulator or the device, then we must swap the width and the height of the view, as the width becomes height and the height becomes width.

1   float viewWidth = self.view.frame.size.width;
2   float viewHeight = self.view.frame.size.height;

The next important task now. What we want from our app is to scroll the scroll view just enough to reveal the textfield into the viewable area of the screen, after the keyboard gets shown (and vice versa of course). To achieve this, we must implement five small steps:

  1. To get the keyboard size using the parameter’s notification object.
  2. To set the correct values for widths and heights, depending on the orientation.
  3. To get the frame of the remaining viewable area after the keyboard gets shown.
  4. To get the textfield’s frame.
  5. To check if the viewable area contains the textfield and scroll if it doesn’t (yes, we’ll implement this checking even though we know that the viewable area doesn’t contain our textfield).

Here we go. The notification object that the iOS sends contains a NSDictionary, named userInfo. From this, we’ll find the size of the keyboard:

1   /** STEP 1 **/
2   // Get the size of the keyboard from the userInfo dictionary.
3   NSDictionary *keyboardInfo = [notification userInfo];
4   CGSize keyboardSize = [[keyboardInfo objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;

We will store the keyboard height into a variable, because as a height we’ll set its width if the orientation is landscape.

1   float keyboardHeight = keyboardSize.height;

Now we have to check the orientation of the device. If it’s in landscape mode, then we must swap the widths and heights as follows:

1   if (UIDeviceOrientationIsLandscape([[UIDevice currentDevice] orientation])) {
2          keyboardHeight = keyboardSize.width;
3   
4          // Also swap the view width and height.
5          float temp = viewWidth;
6          viewWidth = viewHeight;
7          viewHeight = temp;
8     }

Next, let’s calculate the frame of the remaining viewable area.

1   CGRect viewableAreaFrame = CGRectMake(0.0, 0.0, viewWidth, viewHeight - keyboardHeight);

For the step 4, we get the textfield’s frame:

1   CGRect txtDemoFrame = txtDemo.frame;

Finally, we arrive at the fifth step of what we need to do in this method. At first, we check if the viewable area contains the textfield (we know it doesn’t, but we do this to be 100% correct and to have correct code for later use). If it doesn’t, we calculate the offset point for the scroll view, we store the current offset and… scroll.

1   if (!CGRectContainsRect(viewableAreaFrame, txtDemoFrame)) {
2       // We need to calculate the new Y offset point.
3       float scrollPointY = viewHeight - keyboardHeight;
4   
5       // Don't forget that the scroller should go to its original position
6       // so store the current Y point of the content offset.
7       originalScrollerOffsetY = scroller.contentOffset.y;
8   
9       // Finally, scroll.
10      [scroller setContentOffset:CGPointMake(0.0, scrollPointY) animated:YES];
11  }

There you go! Our method is ready! As you can see, we scroll using the setContentOffsets:animated: method of the scroll view.

Let’s implement the keyboardWillHide method. Don’t worry, it’s only two lines of code:

1   -(void)keyboardWillHide:(NSNotification *)notification{
2       NSLog(@"Keyboard is closing.");
3   
4       // Move the scroll view back into its original position.
5       [scroller setContentOffset:CGPointMake(0.0, originalScrollerOffsetY) animated:YES];
6   }

I hope from all the previous code that you understood why we declared the originalScrollerOffsetY variable in the private section of the interface.

Are we finished? Well, not yet. Two things are remaining. At first, we need to implement the textfieldShouldReturn: delegate method, if we want the keyboard to be able to hide.

1   -(BOOL)textFieldShouldReturn:(UITextField *)textField{
2       [txtDemo resignFirstResponder];
3   
4       return YES;
5   }

What else? You can find out what else if you try to run the app now. If you keep it in portrait mode without changing the orientation, it will work perfect. But what is going to happen if you set the orientation in landscape mode? Not what we want to happen, that’s for sure. So, let’s fix it.

In every orientation we need two things to be correct. The content size of the scroll view and the initial position of the textfield (you cannot have the textfield in the same position in both portrait and landscape modes). Go to the shouldAutorotateToInterfaceOrientation: method that it automatically produced by the Xcode and add this code:

1   if (interfaceOrientation == UIInterfaceOrientationPortrait ||
2       interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown) {
3       NSLog(@"Portrait orientation.");
4   
5       // Set the scroller's content size.
6       scroller.contentSize = CGSizeMake(320.0, 600.0);
7   
8       // Set the original frame of our textfield, centered on the screen.
9       [txtDemo setFrame:CGRectMake(20.0, 350.0, 280.0, 31.0)];
10  }
11  else{
12      NSLog(@"Landscape orientation");
13  
14      // Set the scroller's content size.
15      scroller.contentSize = CGSizeMake(480.0, 600.0);
16  
17      // Set the original frame of our textfield, centered on the screen.
18      // Don't forget that in landscape mode the height of the screen is considered as the width.
19      // The value 140.0 is the textfield's width/2 (280.0/2).
20      [txtDemo setFrame:CGRectMake([UIScreen mainScreen].bounds.size.height / 2 - 140.0, 200.0, 280.0, 31.0)];
21  }

Also, comment or delete this line at the end of the method:

1   return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);

and write this one:

1   return YES;

Here is the method completed:

1   - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
2   {
3       if (interfaceOrientation == UIInterfaceOrientationPortrait || interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown) {
4           NSLog(@"Portrait orientation.");
5   
6           // Set the scroller's content size.
7           scroller.contentSize = CGSizeMake(320.0, 600.0);
8   
9           // Set the original frame of our textfield, centered on the screen.
10          [txtDemo setFrame:CGRectMake(20.0, 350.0, 280.0, 31.0)];
11      }
12      else{
13          NSLog(@"Landscape orientation");
14  
15          // Set the scroller's content size.
16          scroller.contentSize = CGSizeMake(480.0, 600.0);
17  
18          // Set the original frame of our textfield, centered on the screen.
19          // Don't forget that in landscape mode the height of the screen is considered as the width.
20          // The value 140.0 is the textfield's width/2 (280.0/2).
21          [txtDemo setFrame:CGRectMake([UIScreen mainScreen].bounds.size.height / 2 - 140.0, 200.0, 280.0, 31.0)];
22      }
23  
24      //return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
25  
26      return YES;
27  }

STEP 5 – Test the app PAGE TOP

We are ready. You can test the app in the Simulator. Show and hide the keyboard, change the orientation and look how your app receives the notifications and knows when the keyboard is about to be shown or to be closed and how the hidden textfield gets revealed and returns back into its original position. Don’t forget to see the messages on the debugger too.

Here are some screenshots of it. Not much stuff, as the interface is quite simple.

naren.JPG

The project and the demo sound file can be downloaded at the Downloads area of this website.

This tutorial was transferred as it was originally written in my blog, therefore some techniques, tools or SDKs may have changed since then.

Reference PAGE TOP

http://gtiapps.com/?p=817

0 Comments Leave a comment

Please login in order to leave a comment.

Newest first
!

Sign-in to your Chupamobile Account.

The Easiest way to Launch your next App or Game.

Join Chupamobile and get instant access to thousands of ready made App and Game Templates.

Creating an account means you’re okay with Chupamobile’s Terms of Service and Privacy Policy.