Actually In my iPhone app i used the following code to lift the view in order to show the textfield above the keyboard. My application support all four interface orientations. I use the following code to lift the view. Problem here is that it only works for portrait orientation. Is there any way to implement the correct lifting in all four orientations
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;
- (void)textFieldDidBeginEditing:(UITextField *)textField {
UIInterfaceOrientation orientation = [Globals getUIInterfaceOrientation];//[[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];
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{
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];
}
You can try setting the textField’s inputAccessoryView property to itsself. I haven't tried this, but in theory it should make the textField attach itself to the keyboard and thus always be displayed:
textField.inputAccessoryView = textField;
Related
I have a UITextfield in which I am entering value. I don't want to scroll myself or adjust keyboard myself , I want Keyboard automatically adjust itself when I enter text in UITextfield and always show below the textfield.
Any hints from experts would be very welcome.
Here is some code that I have used. Basically, what we're going to do is animate the position of the view whenever a UITextField gets focus. To do this, we must make our UIViewController a delegate of our UITextFields
define a variable float animatedDistance in .h of the file
#interface <ClassName>
float animatedDistance;
#end
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;
- (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];
}
}
Now that the view has been 'pushed up' when a UITextField is selected, we want to make sure that we slide it back down when we're done:
- (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];
}
For more Visit Apple Documentation
Animate the frame (viewcontroller.view.frame.origin.y - 215) of your view that contains the textfield when the textfield starts editing and back down when it is finished.
Use the UITextfieldDelegate methods
You can use this class which will help you to do the needful. All you need to do is make the class of that UITableView to TPKeyboardAvoidingTableView. Same for UIScrollView and UICollectionView. You can used these classes to avoid the keyboard.
Hope this helps. And if it does, please accept. ;)
Thanks and Cheers.
I have Custom UIView class in one textview. Custom View class frame set only some one view controller. My thought is when text view delegate methods textviewdidbeginediting animation of textview based on the keyboard height not worked correctly. I am using following code
static const CGFloat KEYBOARD_ANIMATION_DURATION = 0.3;
static const CGFloat MINIMUM_SCROLL_FRACTION = 0.2;
static const CGFloat MAXIMUM_SCROLL_FRACTION = 0.2;
static const CGFloat PORTRAIT_KEYBOARD_HEIGHT = 216;
static const CGFloat LANDSCAPE_KEYBOARD_HEIGHT = 162;
- (void)textViewDidBeginEditing:(UITextView *)textView {
CGRect textFieldRect=
[self.window convertRect:textView.bounds fromView:textView];
CGRect viewRect =[self.window convertRect:self.bounds fromView:self];
//So now we have the bounds, we need to calculate the fraction between the top and bottom of the middle section for the text field's midline:
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;
//Clamp this fraction so that the top section is all "0.0" and the bottom section is all "1.0".
if (heightFraction < 0.0)
{
heightFraction = 0.0;
}
else if (heightFraction > 1.0)
{
heightFraction = 1.0;
}
// Now take this fraction and convert it into an amount to scroll by multiplying by the keyboard height for the current screen orientation. Notice the calls to floor so that we only scroll by whole pixel amounts.
UIInterfaceOrientation orientation =
[[UIApplication sharedApplication] statusBarOrientation];
if (orientation == UIInterfaceOrientationPortrait ||orientation == UIInterfaceOrientationPortraitUpsideDown)
{
animatedDistance = floor(PORTRAIT_KEYBOARD_HEIGHT * heightFraction);
}
else
{
animatedDistance = floor(LANDSCAPE_KEYBOARD_HEIGHT * heightFraction);
}
//Finally, apply the animation. Note the use of setAnimationBeginsFromCurrentState: — this will allow a smooth transition to new text field if the user taps on another.
CGRect viewFrame = self.frame;
viewFrame.origin.y -= animatedDistance;
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationDuration:KEYBOARD_ANIMATION_DURATION];
[self setFrame:viewFrame];
[UIView commitAnimations];
}
-(void)textViewDidEndEditing:(UITextView *)textView{
CGRect viewFrame = self.frame;
viewFrame.origin.y += animatedDistance;
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationDuration:KEYBOARD_ANIMATION_DURATION];
[self setFrame:viewFrame];
[UIView commitAnimations];
}
and also attached my screen shots
how to solve this issue
You and move your textView using animation ,
here is the code , you can set frame according to you
-(void)makeAnimation
{
yourView.alpha = 0;
CGRect optionsFrame = yourView.frame;
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:.5];
CGRect optionsFrame2 = {0,0,320,266};
yourView.frame = optionsFrame2;
yourView.alpha=1;
[UIView commitAnimations];
}
I am trying to scroll through textarea fields in the view using a next button on top of the keyboard, when it shifts to the last textarea field it's scrolling downwards and hiding behind the keyboard.
code:
(CGRect)getObjectRectFromRoot {
int yOffset = 0;
yOffset += self.view.frame.origin.y;
UIView* suView = self.view.superview;
while (suView) {
yOffset += suView.frame.origin.y;
suView = suView.superview;
}
CGRect r;
r.size.width = 700;
r.size.height = 700;
r.size.width = self.view.frame.size.width;
r.size.height = self.view.frame.size.height;
r.origin.x = 0;
r.origin.y = yOffset;
return r;
}
Implementing this code in the two delegate methods of your textField will do what you want. Here is the textFieldDidBeginEditing:
- (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];
}
And here is the textFieldDidEndEditing:
- (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];
[self textFieldShouldReturn:textField];
[UIView commitAnimations];
}
If you want it for the buttons you can just setup an IBAction with the above code.
The code can also be used with any other type of action or view you would like!
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.
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?