ScrollView Example in iPhone

In this application we will see how to TextField implement in the ScrollView. So let see how it will worked. Another ScrollView example you can find out from here ScrollView Example

Step 1: Open the Xcode, Create a new project using View Base application. Give the application “ScrollView_Example”.

Step 2: Xcode automatically creates the directory structure and adds essential frameworks to it. You can explore the directory structure to check out the content of the directory.

Step 3: Open the ScrollView_ExampleViewController.h file and make the following changes:

#import <UIKit/UIKit.h>

@interface ScrollView_ExampleViewController : UIViewController {

IBOutlet UIScrollView *scrollView;

IBOutlet UITextField *textfield1;
IBOutlet UITextField *textfield2;
IBOutlet UITextField *textfield3;
IBOutlet UITextField *textfield4;
IBOutlet UITextField *textfield5;
IBOutlet UITextField *textfield6;
IBOutlet UITextField *textfield7;
IBOutlet UITextField *textfield8;

}

@end

Step 4: Double click the ScrollView_ExampleViewController.xib file and open it to the Interface Builder. First drag the ScrollView from the library and place it to the view window. Next drag eight Label and eight UITextField from the library and place it to the view window (See the figure 1). Select the ScrollView from the view and bring up Size Inspector and changed the y position (-200) (See figure 2). Now Save the ScrollView_ExampleViewController.xib file , close it and go back to the Xcode.


Figure 1


Figure 2

Step 5: In the ScrollView_ExampleViewController.m file make the following changes:

#import "ScrollView_ExampleViewController.h"

@implementation ScrollView_ExampleViewController

(void)dealloc
{
[super dealloc];
}

(void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];

}

#pragma mark – View lifecycle

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
(void)viewDidLoad
{

[super viewDidLoad];

scrollView.frame = CGRectMake(0, 0, 320, 460);
[scrollView setContentSize:CGSizeMake(320, 678)];

}

(BOOL)textFieldShouldReturn:(UITextField *)textField{
[textField resignFirstResponder];
return YES;
}

(void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}

(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

@end

Step 6: Now compile and run the application on the Simulator.

You can Download SourceCode from here ScrollView_Example

Leave a Reply

Your email address will not be published. Required fields are marked *