Objective-C: UIScroller and UItextfields hidden by keyboard appearing in UIViewController - iphone

I need help trying to configure in a UIViewController, the keyboard : (a) not hovering over both UITextfields, so the scroller should be positioned correctly; and (b) When the user touches the background the keyboard disappears.
I haven't tried (b) yet but I'm trying (a) and the code I got from googling around didn't give me the desired effects. In-fact, it makes my textboxes disappear when I touch one of them. Im sure my implementation of activeField is also wrong. I got this example from Apple Development's Keyboard section.
The code I have so far is below:
#interface FirstViewController : UIViewController {
IBOutlet UITextField *emailAddress;
IBOutlet UITextField *password;
IBOutlet UIButton *loginButton;
IBOutlet UIScrollView *scroller;
BOOL keyboardShown;
UITextField *activeField;
ASIHTTPRequest *requestRequiringAuthentication;
ASINetworkQueue *networkQueue;
}
- (IBAction) LoginUser:(id)sender;
#property (nonatomic,retain) IBOutlet UITextField *emailAddress;
#property (nonatomic,retain) IBOutlet UITextField *password;
#property (nonatomic, retain) IBOutlet UIScrollView *scroller;
#property (nonatomic, retain) UITextField *activeField;
#property (retain) ASINetworkQueue *networkQueue;
#property (retain) ASIHTTPRequest *requestRequiringAuthentication;
#end
#implementation FirstViewController
#synthesize requestRequiringAuthentication;
#synthesize networkQueue;
#synthesize password;
#synthesize emailAddress;
#synthesize scroller;
#synthesize activeField;
#pragma mark -
#pragma mark LifeCycle
- (void)awakeFromNib
{
NSLog(#"awaking from nib");
[self setNetworkQueue:[[[ASINetworkQueue alloc] init] autorelease]];
}
- (void)viewDidLoad {
NSLog(#"viewdidload");
[super viewDidLoad];
}
- (void)viewDidUnload {
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void)viewWillAppear:(BOOL)animated {
keyboardShown = NO;
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(keyboardWasShown:) name:UIKeyboardDidShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(keyboardWasHidden:) name:UIKeyboardDidHideNotification object:nil];
[super viewWillAppear:animated];
}
- (void)viewWillDisappear:(BOOL)animated {
[[NSNotificationCenter defaultCenter] removeObserver:self];
[super viewWillDisappear:animated];
}
// Called when the UIKeyboardDidShowNotification is sent.
- (void)keyboardWasShown:(NSNotification*)aNotification
{
if (keyboardShown)
return;
NSDictionary* info = [aNotification userInfo];
// Get the size of the keyboard.
NSValue* aValue = [info objectForKey:UIKeyboardBoundsUserInfoKey];
CGSize keyboardSize = [aValue CGRectValue].size;
// Resize the scroll view (which is the root view of the window)
CGRect viewFrame = [scroller frame];
viewFrame.size.height -= keyboardSize.height;
scroller.frame = viewFrame;
// Scroll the active text field into view.
CGRect textFieldRect = [activeField frame];
[scroller scrollRectToVisible:textFieldRect animated:YES];
keyboardShown = YES;
}
// Called when the UIKeyboardDidHideNotification is sent
- (void)keyboardWasHidden:(NSNotification*)aNotification
{
NSDictionary* info = [aNotification userInfo];
// Get the size of the keyboard.
NSValue* aValue = [info objectForKey:UIKeyboardBoundsUserInfoKey];
CGSize keyboardSize = [aValue CGRectValue].size;
// Reset the height of the scroll view to its original value
CGRect viewFrame = [scroller frame];
viewFrame.size.height += keyboardSize.height;
scroller.frame = viewFrame;
keyboardShown = NO;
}
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
activeField = textField;
}
- (void)textFieldDidEndEditing:(UITextField *)textField
{
activeField = nil;
}

I found the following code in a blog entry (I don't know which one because I think I had it saved in NNW before the switch to Google Reader syncing). It all lives in the implementation file, just make your class the delegate for your UITextFields, and declare it as following the UITextFieldDelegate protocol in your header. I pulled this straight out of the code for one of my shipping apps, and it works great for moving the text fields out of the way of the keyboard. I haven't setup your (B) yet, but I believe it's just another UITextFieldDelegate method that needs to be implemented.
Setup the following constants for your class:
static const CGFloat KEYBOARD_ANIMATION_DURATION = 0.3;
static const CGFloat MINIMUM_SCROLL_FRACTION = 0.2;
static const CGFloat MAXIMUM_SCROLL_FRACTION = 0.8;
static const CGFloat PORTRAIT_KEYBOARD_HEIGHT = 216;
static const CGFloat LANDSCAPE_KEYBOARD_HEIGHT = 140;
And then add these two methods:
- (void)textFieldDidBeginEditing:(UITextField *)textField {
CGRect textFieldRect = [self.view.window convertRect:textField.bounds fromView:textField];
CGRect viewRect = [self.view.window convertRect:self.view.bounds fromView:self.view];
CGFloat midline = textFieldRect.origin.y + 0.5 * textFieldRect.size.height;
CGFloat numerator =
midline - viewRect.origin.y - MINIMUM_SCROLL_FRACTION * viewRect.size.height;
CGFloat denominator =
(MAXIMUM_SCROLL_FRACTION - MINIMUM_SCROLL_FRACTION) * viewRect.size.height;
CGFloat heightFraction = numerator / denominator;
if (heightFraction < 0.0)
{
heightFraction = 0.0;
}
else if (heightFraction > 1.0)
{
heightFraction = 1.0;
}
UIInterfaceOrientation orientation =
[[UIApplication sharedApplication] statusBarOrientation];
if (orientation == UIInterfaceOrientationPortrait ||
orientation == UIInterfaceOrientationPortraitUpsideDown)
{
animatedDistance = floor(PORTRAIT_KEYBOARD_HEIGHT * heightFraction);
}
else
{
animatedDistance = floor(LANDSCAPE_KEYBOARD_HEIGHT * heightFraction);
}
CGFloat heightFraction = numerator / denominator;
if (heightFraction < 0.0)
{
heightFraction = 0.0;
}
else if (heightFraction > 1.0)
{
heightFraction = 1.0;
}
UIInterfaceOrientation orientation =
[[UIApplication sharedApplication] statusBarOrientation];
if (orientation == UIInterfaceOrientationPortrait ||
orientation == UIInterfaceOrientationPortraitUpsideDown)
{
animatedDistance = floor(PORTRAIT_KEYBOARD_HEIGHT * heightFraction);
}
else
{
animatedDistance = floor(LANDSCAPE_KEYBOARD_HEIGHT * heightFraction);
}
CGRect viewFrame = self.view.frame;
viewFrame.origin.y -= animatedDistance;
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationDuration:KEYBOARD_ANIMATION_DURATION];
[self.view setFrame:viewFrame];
[UIView commitAnimations];
}
- (void)textFieldDidEndEditing:(UITextField *)textField {
CGRect viewFrame = self.view.frame;
viewFrame.origin.y += animatedDistance;
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationDuration:KEYBOARD_ANIMATION_DURATION];
[self.view setFrame:viewFrame];
[UIView commitAnimations];
}

I have been looking around for something similar in my case in which the password field was hidden by the keyboard. I wanted to resolve my problem with minimal lines of code.
This solution could also work with more text fields, but just detect each field and apply the offset needed.
In my case
1) I first placed the username, password and sign in button inside a scrollView.
2) Made the viewController a UITextFieldDelegate
3) Wrote the following lines of code (make sure your scrollView is an IBOutlet and it's connected from Interface Builder)
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
if ([textField isEqual:textAccountPassword] == NO)
return;
CGPoint bottomOffset = CGPointMake(0, 20);
[scrollview setContentOffset: bottomOffset animated: YES];
}
- (void)textFieldDidEndEditing:(UITextField *)textField
{
CGPoint bottomOffset = CGPointMake(0, 0); // return it back to no offset
[scrollview setContentOffset: bottomOffset animated: YES];
}

The right code follows:
static const CGFloat KEYBOARD_ANIMATION_DURATION = 0.3;
static const CGFloat MINIMUM_SCROLL_FRACTION = 0.0;
static const CGFloat MAXIMUM_SCROLL_FRACTION = 0.2;
static const CGFloat PORTRAIT_KEYBOARD_HEIGHT = 216;
static const CGFloat LANDSCAPE_KEYBOARD_HEIGHT = 240;
- (void)textFieldDidBeginEditing:(UITextField *)textField {
CGRect textFieldRect = [self.view.window convertRect:textField.bounds
fromView:textField];
CGRect viewRect = [self.view.window convertRect:self.view.bounds fromView:self.view];
CGFloat midline = textFieldRect.origin.y + 0.5 * textFieldRect.size.height;
CGFloat numerator =
midline - viewRect.origin.y - MINIMUM_SCROLL_FRACTION * viewRect.size.height;
CGFloat denominator =
(MAXIMUM_SCROLL_FRACTION - MINIMUM_SCROLL_FRACTION) * viewRect.size.height;
CGFloat heightFraction = numerator / denominator;
if (heightFraction < 0.0)
{
heightFraction = 0.0;
}
else if (heightFraction > 1.0)
{
heightFraction = 1.0;
}
UIInterfaceOrientation orientation =
[[UIApplication sharedApplication] statusBarOrientation];
if (orientation == UIInterfaceOrientationPortrait ||
orientation == UIInterfaceOrientationPortraitUpsideDown)
{
animatedDistance = floor(PORTRAIT_KEYBOARD_HEIGHT * heightFraction);
}
else
{
animatedDistance = floor(LANDSCAPE_KEYBOARD_HEIGHT * heightFraction);
}
heightFraction = numerator / denominator;
if (heightFraction < 0.0)
{
heightFraction = 0.0;
}
else if (heightFraction > 1.0)
{
heightFraction = 1.0;
}
orientation =
[[UIApplication sharedApplication] statusBarOrientation];
if (orientation == UIInterfaceOrientationPortrait ||
orientation == UIInterfaceOrientationPortraitUpsideDown)
{
animatedDistance = floor(PORTRAIT_KEYBOARD_HEIGHT * heightFraction);
}
else
{
animatedDistance = floor(LANDSCAPE_KEYBOARD_HEIGHT * heightFraction);
}
CGRect viewFrame = self.view.frame;
viewFrame.origin.y -= animatedDistance;
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationDuration:KEYBOARD_ANIMATION_DURATION];
[self.view setFrame:viewFrame];
[UIView commitAnimations];
}
- (void)textFieldDidEndEditing:(UITextField *)textField {
CGRect viewFrame = self.view.frame;
viewFrame.origin.y += animatedDistance;
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationDuration:KEYBOARD_ANIMATION_DURATION];
[self.view setFrame:viewFrame];
[UIView commitAnimations];
}
Adjust the static constants to fit your needs.

I think the easiest way to solve this issue is to shift the view up and down, corresponding to the keyboard
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
CGRect rect = self.view.frame;
rect = CGRectMake(rect.origin.x, rect.origin.y,
rect.size.width, rect.size.height + 80);//keyboard height
[UIView beginAnimations: #"anim" context: nil];
[UIView setAnimationBeginsFromCurrentState: YES];
[UIView setAnimationDuration: movementDuration];
self.view.frame = rect;
[UIView commitAnimations];
}
- (void)textFieldDidEndEditing:(UITextField *)textField
{
//same as above but you shift it down
}

Related

How to move the focussed text field to the middle of the view after keyboard popups

In my iPad im creating SplitView application. I have a UITableVewtable with multiple rows, each row/cell having three UITextField
When i tap on UITextField some fields are hide by the keyboard.
So im using TPKeyboardAvoidingScrollView framework but it does not works on ios 5.0+.
some times unable to scroll the scroll view on the table.
So I want to move the focussed cell to the middle/ just above the key board at every time
What should I use?
Thanks in Advance.
Try this code. Put this in your .h file :
CGFloat animatedDistance;
In your .m file :
static const CGFloat KEYBOARD_ANIMATION_DURATION = 0.3;
static const CGFloat MINIMUM_SCROLL_FRACTION = 0.2;
static const CGFloat MAXIMUM_SCROLL_FRACTION = 0.8;
static const CGFloat PORTRAIT_KEYBOARD_HEIGHT = 264;
static const CGFloat LANDSCAPE_KEYBOARD_HEIGHT = 352;
Add this static const before your #interface myclass () You can change the keyboard height values above as per your need. And then add the following code :
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
CGRect textFieldRect =
[self.view.window convertRect:textField.bounds fromView:textField];
CGRect viewRect =
[self.view.window convertRect:self.view.bounds fromView:self.view];
CGFloat midline = textFieldRect.origin.y + 0.5 * textFieldRect.size.height;
CGFloat numerator = midline - viewRect.origin.y - MINIMUM_SCROLL_FRACTION * viewRect.size. height;
CGFloat denominator =
(MAXIMUM_SCROLL_FRACTION - MINIMUM_SCROLL_FRACTION) * viewRect.size.height;
CGFloat heightFraction = numerator / denominator;
if (heightFraction < 0.0)
{
heightFraction = 0.0;
}
else if (heightFraction > 1.0)
{
heightFraction = 1.0;
}
UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
if (orientation == UIInterfaceOrientationPortrait ||
orientation == UIInterfaceOrientationPortraitUpsideDown)
{
animatedDistance = floor(PORTRAIT_KEYBOARD_HEIGHT * heightFraction);
}
else
{
animatedDistance = floor(LANDSCAPE_KEYBOARD_HEIGHT * heightFraction);
}
CGRect viewFrame = self.view.frame;
viewFrame.origin.y -= animatedDistance;
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationDuration:KEYBOARD_ANIMATION_DURATION];
[self.view setFrame:viewFrame];
[UIView commitAnimations];
}
- (void)textFieldDidEndEditing:(UITextField *)textField
{
CGRect viewFrame = self.view.frame;
viewFrame.origin.y += animatedDistance;
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationDuration:KEYBOARD_ANIMATION_DURATION];
[self.view setFrame:viewFrame];
[UIView commitAnimations];
}
what you want is to animate your view such as when you touch the textField it should go up and when you end typing it should go down. You can achieve this by implementing two UITextFiled delegate methods and some little animation as follows :
- (void)textFieldDidEndEditing:(UITextField *)textField
{
[self startAnimatingTextField: textField up: NO];
}
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
[self startAnimatingTextField: textField up: YES];
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationDuration:0.1];
[UIView setAnimationCurve: UIViewAnimationCurveLinear];
[UIView commitAnimations];
}
-(void)startAnimatingTextField:(UITextField *) textField up: (BOOL) up
{
const int distance = 120; /* modify conform your needs */
const float duration = 0.3f;
int movement = (up ? -movementDistance : movementDistance);
[UIView beginAnimations: #"anim" context: nil];
[UIView setAnimationBeginsFromCurrentState: YES];
[UIView setAnimationDuration: movementDuration];
self.view.frame = CGRectOffset(self.view.frame, 0, movement);
[UIView commitAnimations];
}
Hope this helps !
First open the xib file and set the Content insets(In Scrollview Size) to 200
Now update following two methods as below in .m file.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cell=#"Cell";
Cell *customCell= (Cell *)[tableView dequeueReusableCellWithIdentifier:cell];
if (customCell==nil) {
NSArray *bundle = [[NSBundle mainBundle] loadNibNamed:#"Cell" owner:self options:nil];
for (id object in bundle) {
if ([object isKindOfClass:[Cell class]])
{
customCell = (Cell *)object;
break;
}
}
}
customCell.IBTxtfield.tag=indexPath.row;
return customCell;
}
- (void)textFieldDidBeginEditing:(UITextField *)textField{
NSIndexPath *path=[NSIndexPath indexPathForRow:textField.tag inSection:0];
[IBtblView scrollToRowAtIndexPath:path atScrollPosition:UITableViewScrollPositionTop animated:YES];
}

Multiple UITextFields with UIPicker and UIKeyboard

So I have about 7 textfields and 6 of them use a keyboard and the other uses a picker. The problem I'm having is that if the keyboard is open when the textfield that is linked to the picker is touched the keyboard won't dismiss and the picker appears under it. Here's my code
- (void) textFieldDidBeginEditing:(UITextField *)textField
{
pickerView.hidden = YES;
if ([textField isEqual:state])
{
[state resignFirstResponder];
[self textFieldFirstResponderOnDelay1];
}
else
{
pickerView.hidden = YES;
// This movie the view up so textfield isn't hidden by keyboard
CGRect textFieldRect =
[self.view.window convertRect:textField.bounds fromView:textField];
CGRect viewRect =
[self.view.window convertRect:self.view.bounds fromView:self.view];
CGFloat midline = textFieldRect.origin.y + 0.5 * textFieldRect.size.height;
CGFloat numerator =
midline - viewRect.origin.y
- MINIMUM_SCROLL_FRACTION * viewRect.size.height;
CGFloat denominator =
(MAXIMUM_SCROLL_FRACTION - MINIMUM_SCROLL_FRACTION)
* viewRect.size.height;
CGFloat heightFraction = numerator / denominator;
if (heightFraction < 0.0)
{
heightFraction = 0.0;
}
else if (heightFraction > 1.0)
{
heightFraction = 1.0;
}
UIInterfaceOrientation orientation =
[[UIApplication sharedApplication] statusBarOrientation];
if (orientation == UIInterfaceOrientationPortrait ||
orientation == UIInterfaceOrientationPortraitUpsideDown)
{
animatedDistance = floor(PORTRAIT_KEYBOARD_HEIGHT * heightFraction);
}
else
{
animatedDistance = floor(LANDSCAPE_KEYBOARD_HEIGHT * heightFraction);
}
CGRect viewFrame = self.view.frame;
viewFrame.origin.y -= animatedDistance;
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationDuration:KEYBOARD_ANIMATION_DURATION];
[self.view setFrame:viewFrame];
[UIView commitAnimations];
}
}
-(void) viewDidLoad
{
pickerView = [[UIPickerView alloc] init];
pickerView.frame = CGRectMake(0, 245, 320, 216);
pickerView.delegate = self;
pickerView.hidden = YES;
pickerView.showsSelectionIndicator = YES;
state.inputView = pickerView;
[self.view addSubview:pickerView];
}
-(void)textFieldFirstResponderOnDelay1
{
pickerView.hidden=NO;
[pickerView reloadAllComponents];
}
- (void)textFieldDidEndEditing:(UITextField *)textField
{
if([textField isEqual:state])
{
}
else
{
CGRect viewFrame = self.view.frame;
viewFrame.origin.y += animatedDistance;
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationDuration:KEYBOARD_ANIMATION_DURATION];
[self.view setFrame:viewFrame];
[UIView commitAnimations];
}
}
Instead of adding the pickerview as a subview of your main window, just set it as the input view for the appropriate textviews, then it will show/hide as the keyboard would normally:
textField.inputView = pickerView;

Trigger done button touch in iPhone

How can i trigger done button touch in iPhone?
I have an asynchronous task which pops me small tableView (adds it to subView). When one of my textFields is focused - keyboard is shown and overlays my tableView.
I tried to call resignFirstResponder on focused textField in "textFieldShouldBeginEditing" and it didn't help. So i think maybe explicit trigger of done button will work.
You can animate the entire frame of the view whenever the textfield is focused, so that it slides up and display the table and also the textfield without any hindrance.
you could have the done button as right navigation button of your view controller.
UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithTitle:#"Done" style:UIBarButtonItemStylePlain target:self action:#selector(doneButtonPressed:)];
self.navigationItem.rightBarButtonItem = doneButton ;
[doneButton release];
Implement doneButtonPressed: method as below.
-(void) doneButtonPressed:(id) sender
{
[myTextField resignFirstResponder];
}
If you don't want the text field to becomeFirstResponder or to be edited, return NO from textFieldShouldBeginEditing method.
What I understood from your question that your other fields get hided when you enter in the textfield.For this you need to animate the view.I am going to put the code for this for you.
In your header file declare this.
CGFloat animatedDistance;
And in your .m file put this code and you won't face any problem.Your view will automatically scroll up and down when you start editing the text fields so making the space to view your fields properly..
#pragma mark -
#pragma mark (Text Field Delegate Method)
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
return YES;
}
-(void)textFieldDidBeginEditing:(UITextField *)textField
{
static const CGFloat KEYBOARD_ANIMATION_DURATION = 0.3;
static const CGFloat MINIMUM_SCROLL_FRACTION = 0.2;
static const CGFloat MAXIMUM_SCROLL_FRACTION = 0.8;
static const CGFloat PORTRAIT_KEYBOARD_HEIGHT = 216;
static const CGFloat LANDSCAPE_KEYBOARD_HEIGHT = 162;
CGRect textFieldRect;
CGRect viewRect;
textFieldRect =[addProScrollView.window convertRect:textField.bounds fromView:textField];
viewRect =[addProScrollView.window convertRect:addProScrollView.bounds fromView:addProScrollView];
CGFloat midline = textFieldRect.origin.y + 0.5 * textFieldRect.size.height;
CGFloat numerator = midline - viewRect.origin.y - MINIMUM_SCROLL_FRACTION * viewRect.size.height;
CGFloat denominator = (MAXIMUM_SCROLL_FRACTION - MINIMUM_SCROLL_FRACTION) * viewRect.size.height;
CGFloat heightFraction = numerator / denominator;
if (heightFraction < 0.0)
{
heightFraction = 0.0;
}
else if (heightFraction > 1.0)
{
heightFraction = 1.0;
}
UIInterfaceOrientation orientation =[[UIApplication sharedApplication] statusBarOrientation];
if (orientation == UIInterfaceOrientationPortrait ||orientation == UIInterfaceOrientationPortraitUpsideDown)
{
animatedDistance = floor(PORTRAIT_KEYBOARD_HEIGHT * heightFraction);
}
else
{
animatedDistance = floor(LANDSCAPE_KEYBOARD_HEIGHT * heightFraction);
}
CGRect viewFrame;
viewFrame= addProScrollView.frame;
viewFrame.origin.y -= animatedDistance;
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationDuration:KEYBOARD_ANIMATION_DURATION];
[addProScrollView setFrame:viewFrame];
[UIView commitAnimations];
}
-(void)textFieldDidEndEditing:(UITextField *)textField
{
if(textField.tag==0)
{
static const CGFloat KEYBOARD_ANIMATION_DURATION = 0.3;
CGRect viewFrame;
viewFrame= addProScrollView.frame;
viewFrame.origin.y += animatedDistance;
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationDuration:KEYBOARD_ANIMATION_DURATION];
[addProScrollView setFrame:viewFrame];
[UIView commitAnimations];
}
}
Thats it .You are now done just run your code.
Cheers......

How to Drag my view a little bit up when keyboard comes up?

In my app i have some textfields which gets filled through the keyboard. There are 6 of them and the last two are in very bottom of the screen and it gets hidden whenever keyboard comes up.
Any suggestions??
Make the textFields delegate as self and add the following code to the textFieldDidBeginEditing and textFieldDidEndEditing.
Define the following constants: (the constants have been set for iPad. Tweak them for iphone)
static const CGFloat KEYBOARD_ANIMATION_DURATION = 0.3;
static const CGFloat MINIMUM_SCROLL_FRACTION = 0.2;
static const CGFloat MAXIMUM_SCROLL_FRACTION = 0.8;
static const CGFloat PORTRAIT_KEYBOARD_HEIGHT = 216;
static const CGFloat LANDSCAPE_KEYBOARD_HEIGHT = 302;
Add two variables in the .h file of your class. CGFloat animatedDistance; and UITextField *currentField;
- (void)textFieldDidBeginEditing:(UITextField *)textField {
currentField = textField;
UIInterfaceOrientation orientation =
[[UIApplication sharedApplication] statusBarOrientation];
CGRect textFieldRect =[self.view.window convertRect:textField.bounds fromView:textField];
CGRect viewRect =[self.view.window convertRect:self.view.bounds fromView:self.view];
if (orientation == UIInterfaceOrientationLandscapeLeft )
{
textFieldRect.origin.y = textFieldRect.origin.x;
textFieldRect.size.height = textFieldRect.size.width;
viewRect.size.height = viewRect.size.width;
}
else if (orientation == UIInterfaceOrientationLandscapeRight )
{
textFieldRect.origin.y =viewRect.size.width - textFieldRect.origin.x;
textFieldRect.size.height = textFieldRect.size.width;
viewRect.size.height = viewRect.size.width;
}
else if (orientation == UIInterfaceOrientationPortraitUpsideDown)
{
textFieldRect.origin.y = viewRect.size.height - textFieldRect.origin.y;
}
CGFloat midline = textFieldRect.origin.y + 0.5 * textFieldRect.size.height;
CGFloat numerator = midline - viewRect.origin.y - MINIMUM_SCROLL_FRACTION * viewRect.size.height;
CGFloat denominator =(MAXIMUM_SCROLL_FRACTION - MINIMUM_SCROLL_FRACTION)* viewRect.size.height;
CGFloat heightFraction = numerator / denominator;
if (heightFraction < 0.0)
{
heightFraction = 0.0;
}
else if (heightFraction > 1.0)
{
heightFraction = 1.0;
}
if (orientation == UIInterfaceOrientationPortrait ||
orientation == UIInterfaceOrientationPortraitUpsideDown)
{
animatedDistance = floor(PORTRAIT_KEYBOARD_HEIGHT * heightFraction);
}
else
{
animatedDistance = floor(LANDSCAPE_KEYBOARD_HEIGHT * heightFraction);
}
CGRect viewFrame = self.view.frame;
viewFrame.origin.y -= animatedDistance;
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationDuration:KEYBOARD_ANIMATION_DURATION];
[self.view setFrame:viewFrame];
[UIView commitAnimations];
}
- (void)textFieldDidEndEditing:(UITextField *)textField {
[textField resignFirstResponder];
CGRect viewFrame = self.view.frame;
viewFrame.origin.y += animatedDistance;
animatedDistance = 0;
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationDuration:KEYBOARD_ANIMATION_DURATION];
[self.view setFrame:viewFrame];
[UIView commitAnimations];
currentField = nil;
}
Use a UIScrollview so the user can scroll down. You can make use of the inputAccessoryView of a UITextField to show buttons like next and previous, on click let the input jump to the next textfield and scroll to it.
Have a look at this question and answers at SO, same problem, different solution.

how UITextField should come up in UIScrollView?

I have a UIScrollView, added as
[self.view addSubview:myScrollView];
There are 8 to 10 UITextFields, so when I click textFields for writing something, they Keyboard comes on them, and they became back to keyboard, now what should I do so that they will look after keyboard end ???
If someone is not clear, may ask again ....
This is how your .h file should look like
#interface LoginScreen : UIViewController <UITextFieldDelegate>
{
...
UITextField *textView;
CGFloat animatedDistance;
}
//put this in your viewcontroller.m file
-(void)textFieldDidBeginEditing:(UITextField *)textField{
static const CGFloat KEYBOARD_ANIMATION_DURATION = 0.3;
static const CGFloat MINIMUM_SCROLL_FRACTION = 0.2;
static const CGFloat MAXIMUM_SCROLL_FRACTION = 0.8;
static const CGFloat PORTRAIT_KEYBOARD_HEIGHT = 216;
static const CGFloat LANDSCAPE_KEYBOARD_HEIGHT = 162;
CGRect textFieldRect =
[self.view convertRect:textField.bounds fromView:textField];
CGRect viewRect =
[self.view convertRect:self.view.bounds fromView:self.view];
CGFloat midline = textFieldRect.origin.y + 0.5 * textFieldRect.size.height;
CGFloat numerator = midline - viewRect.origin.y - MINIMUM_SCROLL_FRACTION * viewRect.size.height;
CGFloat denominator = (MAXIMUM_SCROLL_FRACTION - MINIMUM_SCROLL_FRACTION) * viewRect.size.height;
CGFloat heightFraction = numerator / denominator;
if (heightFraction < 0.0)
{
heightFraction = 0.0;
}
else if (heightFraction > 1.0)
{
heightFraction = 1.0;
}
UIInterfaceOrientation orientation =
[[UIApplication sharedApplication] statusBarOrientation];
if (orientation == UIInterfaceOrientationPortrait ||
orientation == UIInterfaceOrientationPortraitUpsideDown)
{
animatedDistance = floor(PORTRAIT_KEYBOARD_HEIGHT * heightFraction);
}
else
{
animatedDistance = floor(LANDSCAPE_KEYBOARD_HEIGHT * heightFraction);
}
CGRect viewFrame = self.view.frame;
viewFrame.origin.y -= animatedDistance;
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationDuration:KEYBOARD_ANIMATION_DURATION];
[self.view setFrame:viewFrame];
[UIView commitAnimations];
}
- (void)textFieldDidEndEditing:(UITextField *)textField{
static const CGFloat KEYBOARD_ANIMATION_DURATION = 0.3;
CGRect viewFrame = self.view.frame;
viewFrame.origin.y += animatedDistance;
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationDuration:KEYBOARD_ANIMATION_DURATION];
[self.view setFrame:viewFrame];
[UIView commitAnimations];
}
-(BOOL)textFieldShouldReturn:(UITextField *)txtObject {
[txtObject resignFirstResponder];
return YES;
}
Pls go through these
How to make a UITextField move up when keyboard is present?
Keyboard in the way of textfields how to move view up
iPhone Keyboard Covers UITextField
How programmatically move a UIScrollView to focus in a control above keyboard?