keyboard hides textfield for different orientation in ipad - objective-c

In my iPad application, I have few textView and textField's. When I click on textField, the keyboard covers the textField. So I am implementing the below code to move the textview up. But on rotation to portraitUpsideDown its not working fine. It slides the screen down in opposite direction. So how do I solve this problem??
-(void) animateTextField: (UITextView *) textField up: (BOOL) up
{
int txtPosition = (textField.frame.origin.y - 540);
const int movementDistance = (txtPosition < 0 ? 0 : txtPosition); // tweak as needed
const float movementDuration = 0.3f; // tweak as needed
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];
}
-(void)textViewDidBeginEditing:(UITextView *)textField
{
[self animateTextField: textField up: YES];
}
-(void)textViewDidEndEditing:(UITextView *)textField
{
[self animateTextField: textField up: NO];
}
-(BOOL)textFieldShouldReturn:(UITextView *)theTextField
{
[theTextField resignFirstResponder];
return YES;
}

This solution is for iPhone, but it considers both orientations.
You can adapt a bit and voilá:
http://cocoawithlove.com/2008/10/sliding-uitextfields-around-to-avoid.html

Crazy,
Just add another function:
- (void) animateTextView: (UITextView*) textView up: (BOOL) up
{
const int movementDistance = 80; // tweak as needed
const float movementDuration = 0.3f; // tweak as needed
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];
}
Then call it like:
- (void)textViewDidBeginEditing:(UITextView *)textView {
[self animateTextView: textView up: YES];
}
- (void)textViewDidEndEditing:(UITextView *)textView {
[self animateTextView: textView up: NO];
}

If your method like this.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
Try this. I don't know exactly. But I am trying to help you. May be x and y coordinate cannot be changed in any orientation. so try this.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
if(interfaceOrientation=UIInterfaceOrienationPotraitUpsideDown){
//Declare txtPos globally...
txtPos=(textField.frame.origin.y + 540);
}
if(interfaceOrientation=UIInterfaceOrienationPotrait)
{
txtPos=(textField.frame.origin.y - 540);
}
return(YES);
}
in animate method.
assign textPos to txtPosition variable..

You should use keyboard will show and will hide notification to capture keyboard event and adjust your view accordingly.
- (void)dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (void)viewDidLoad {
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(keyboardWillHide:) name:UIKeyboardDidHideNotification object:nil];
}
- (void)keyboardWillShow:(NSNotification *)notification {
CGRect keyboardFrame = [notification.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
CGFloat keyboardHeight = CGRectGetHeight(keyboardFrame);
CGFloat animationDuration = [notification.userInfo[UIKeyboardAnimationDurationUserInfoKey] floatValue];
UIViewAnimationCurve animationCurve = [notification.userInfo[UIKeyboardAnimationCurveUserInfoKey] integerValue];
UIViewAnimationOptions animationOption = animationCurve << 16;
[UIView animateWithDuration:animationDuration delay:0 options:animationOption animations:^{
// adjust height using keyboardHeight
} completion:^(BOOL finished) {
}];
}
- (void)keyboardWillHide:(NSNotification *)notification {
CGRect keyboardFrame = [notification.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
CGFloat keyboardHeight = CGRectGetHeight(keyboardFrame);
CGFloat animationDuration = [notification.userInfo[UIKeyboardAnimationDurationUserInfoKey] floatValue];
UIViewAnimationCurve animationCurve = [notification.userInfo[UIKeyboardAnimationCurveUserInfoKey] integerValue];
UIViewAnimationOptions animationOption = animationCurve << 16;
[UIView animateWithDuration:animationDuration delay:0 options:animationOption animations:^{
// adjust height using keyboardHeight
} completion:^(BOOL finished) {
}];
}
This blog post explains this in detail
http://charlie.cu.cc/2015/10/solution-to-the-ios-software-keyboard-cover-part-of-the-ui/

Related

iOS 7 back swipe gesture and keyboard dismissal

I have a standard UINavigationViewController with a standard UIInteractivePopGestureRecognizer for iOS 7. I also have these well known and widely-used methods:
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
They work like this:
- (void)keyboardWillShow:(NSNotification *)notification{
CGFloat keyboardSlideDuration = [[[notification userInfo] objectForKey: UIKeyboardAnimationDurationUserInfoKey] floatValue];
UIViewAnimationOptions keyboardAnimationCurve = [[notification userInfo][UIKeyboardAnimationCurveUserInfoKey] integerValue]<<16;
if (keyboardIsUp || isAnimating) return;
isAnimating = YES;
[UIView animateWithDuration:keyboardSlideDuration delay:0 options:(keyboardAnimationCurve | UIViewAnimationOptionBeginFromCurrentState) animations:^{
_topConstraint.constant -= kTopConstraintR4Ratio;
[self.view layoutIfNeeded];
}completion:^(BOOL finished){
keyboardIsUp = YES;
isAnimating = NO;
}];
}
- (void)keyboardWillHide:(NSNotification *)notification{
CGFloat keyboardSlideDuration = [[[notification userInfo] objectForKey: UIKeyboardAnimationDurationUserInfoKey] floatValue];
UIViewAnimationOptions keyboardAnimationCurve = [[notification userInfo][UIKeyboardAnimationCurveUserInfoKey] integerValue]<<16;
if (isAnimating) return;
isAnimating = YES;
[UIView animateWithDuration:keyboardSlideDuration delay:0 options:(keyboardAnimationCurve | UIViewAnimationOptionBeginFromCurrentState) animations:^{
_topConstraint.constant = topConstraintOriginalValue;
[self.view layoutIfNeeded];
} completion:^(BOOL finished) {
keyboardIsUp = NO;
isAnimating = NO;
}];
}
The thing is that UIView animateWithDuration is triggered without any respect to duration when I swipe the view back with a gesture. The whole view turns into a mess - it animates slowly when I pop the view, but when the gesture isn't finished - the view jumps up.
It looks like this:
How do I get rid of this strange behavior? I don't want the view to move during the transition and I want it to stay still.
EDIT:
You can download the sample project using the link here:
https://www.dropbox.com/s/034lfy49ru4pelf/SampleProject.zip
Easy hack:
BOOL needAdjustContstrain;
Set needAdjustContstrain to YES in keyboardWillShow:
- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
if (needAdjustContstrain) {
_topContstraint.constant -=40;
}
}
you need to call the layoutIfneeded method before the animation block . Check out the code below :
- (void)keyboardWillHide:(NSNotification *)notification{
CGFloat keyboardSlideDuration = [[[notification userInfo] objectForKey: UIKeyboardAnimationDurationUserInfoKey] floatValue];
UIViewAnimationOptions keyboardAnimationCurve = [[notification userInfo][UIKeyboardAnimationCurveUserInfoKey] integerValue]<<16;
if (isAnimating) return;
isAnimating = YES;
[self.view layoutIfNeeded];
[UIView animateWithDuration:keyboardSlideDuration delay:0 options:(keyboardAnimationCurve | UIViewAnimationOptionBeginFromCurrentState) animations:^{
_topContstraint.constant = 89;
//
} completion:^(BOOL finished) {
keyboardIsUp = NO;
isAnimating = NO;
}];}
Please check the link

UIScrollView scroll issue for UITextViews

I have a form which has multiple textfields and textviews. When keyboad is shown I update scrollview frame to prevent textfields hiding under keyboard.
I did follow this question and implement a similar solution.
It works with uitextfields actually but not for uitextviews.
When keyboard is shown scrollview height's is reduced as much as keyboard height and then scroll to focused uitextfield. But if the focused item is uitextview it just doesnt scroll while height of uiscrollview is reduced.
IB screenshots for my scrollview;
https://www.dropbox.com/s/a1iblh6nsdqy34t/Screenshot%202014-05-30%2021.44.32.png
https://www.dropbox.com/s/o2h8avt5w82r5eo/Screenshot%202014-05-30%2021.45.19.png
Here is the code;
// register for keyboard notifications
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(keyboardWillShow:)
name:UIKeyboardWillShowNotification
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(keyboardDidShow:)
name:UIKeyboardDidShowNotification
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(keyboardWillHide:)
name:UIKeyboardWillHideNotification
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(keyboardDidHide:)
name:UIKeyboardDidHideNotification
object:nil];
Notification handler;
-(void)keyboardWillShow:(NSNotification *)n {
if(keyboardUp)
return;
NSDictionary* userInfo = [n userInfo];
// get the size of the keyboard
CGSize keyboardSize = [[userInfo objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
const int movementDistance = -keyboardSize.height - keyboardPaddingToScrollView;
const float movementDuration = 0.30f;
int movement = movementDistance;
[UIView beginAnimations: #"anim" context: nil];
[UIView setAnimationBeginsFromCurrentState: YES];
[UIView setAnimationDuration: movementDuration];
CGRect contentSize = self.view.frame;
contentSize.size.height += movementDistance;
[self.view setFrame:contentSize];
[UIView commitAnimations];
}
-(void)keyboardDidShow:(NSNotification *)n {
keyboardUp = true;
NSLog(NSStringFromCGRect(self.view.frame));
}
-(void)keyboardWillHide:(NSNotification *)n {
if(!keyboardUp)
return;
NSDictionary* userInfo = [n userInfo];
// get the size of the keyboard
CGSize keyboardSize = [[userInfo objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
const int movementDistance = keyboardSize.height + keyboardPaddingToScrollView;
const float movementDuration = 0.30f;
int movement = movementDistance;
[UIView beginAnimations: #"anim" context: nil];
[UIView setAnimationBeginsFromCurrentState: YES];
[UIView setAnimationDuration: movementDuration];
CGRect contentSize = self.view.frame;
contentSize.size.height += movementDistance;
self.view.frame = contentSize;
[UIView commitAnimations];
}
-(void)keyboardDidHide:(NSNotification *)n {
keyboardUp = false;
}

UIScrollView does not move view ontop of keyboard iPad

I have several text fields on screen. When I tap inside of the one towards the bottom, the keyboard appears over them.
I've referred to apple's documentation and used the following code to try and tackle this.
// Call this method somewhere in your view controller setup code.
- (void)registerForKeyboardNotifications
{
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(keyboardWasShown:)
name:UIKeyboardDidShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(keyboardWillBeHidden:)
name:UIKeyboardWillHideNotification object:nil];
}
// Called when the UIKeyboardDidShowNotification is sent.
- (void)keyboardWasShown:(NSNotification*)aNotification
{
NSDictionary* info = [aNotification userInfo];
CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0, 0.0, kbSize.height, 0.0);
scrollView.contentInset = contentInsets;
scrollView.scrollIndicatorInsets = contentInsets;
// If active text field is hidden by keyboard, scroll it so it's visible
// Your app might not need or want this behavior.
CGRect aRect = self.view.frame;
aRect.size.height -= kbSize.height;
if (!CGRectContainsPoint(aRect, activeField.frame.origin) ) {
[self.scrollView scrollRectToVisible:activeField.frame animated:YES];
}
}
// Called when the UIKeyboardWillHideNotification is sent
- (void)keyboardWillBeHidden:(NSNotification*)aNotification
{
UIEdgeInsets contentInsets = UIEdgeInsetsZero;
scrollView.contentInset = contentInsets;
scrollView.scrollIndicatorInsets = contentInsets;
}
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
activeField = textField;
}
- (void)textFieldDidEndEditing:(UITextField *)textField
{
activeField = nil;
}
What i'd like to happen, when the keyboard appears the text fields that in side the scroll field will move up. Any help is appreciated.
Use This Code for Move the view up..........
-(void) textFieldDidBeginEditing:(UITextField*)textField
{
[self slideViewUpForTextField:textField];
}
-(void) slideViewUpForTextField:(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 + 3.0 * 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 write these on top of the class
And After #import"".....
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 = 158;
static const CGFloat LANDSCAPE_KEYBOARD_HEIGHT = 162;
This is also write ....
-(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];
}

How to move view for keyboard in portrait orientations

I have an app that runs in portrait and portraitUpsideDown only. I need to push the view up when the keyboard appears and pull it back down when it disappears. The following code works perfectly if the device stays in portrait, but if it is in portraitUpsideDown the view moves the wrong direction (-260 instead of 260), plus if the orientation changes while the keyboard is showing, it's not handled... The keyboardWillHide method works fine in both orientations. Is there a way to move the view RELATIVE to the keyboard or status bar so it doesn't matter what orientation the device is in???
- (void) keyboardWillShow:(NSNotification *) notification
{
NSLog(#"Keyboard Will Show");
double animationDuration;
animationDuration = [[[notification userInfo] objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];
[UIView animateWithDuration:animationDuration delay:0 options:UIViewAnimationCurveEaseIn animations:^{
self.view.center = CGPointMake(self.view.center.x, self.view.center.y + -260);
}completion:^(BOOL finished){
}];
}
- (void) keyboardWillHide:(NSNotification *) notification
{
double animationDuration;
animationDuration = [[[notification userInfo] objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];
NSLog(#"Keyboard Will Hide");
[UIView animateWithDuration:animationDuration delay:0 options:UIViewAnimationCurveEaseIn animations:^{
[self.view setFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
}completion:^(BOOL finished){
}];
}
I solved it (less than elegantly) this way:
NOTE: Things you SHOULD do, and I will change this code to do them eventually are: create a property to hold the keyboard animation duration so I can use it outside of the keyboard delegate methods, and similarly create a property for the offset and determine it using the userInfo for the height of the keyboard.
- (void) keyboardWillShow:(NSNotification *) notification
{
NSLog(#"Keyboard Will Show");
if (self.interfaceOrientation == UIInterfaceOrientationPortrait)
offset = -260;
else
offset = 260;
double animationDuration;
animationDuration = [[[notification userInfo] objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];
[UIView animateWithDuration:animationDuration delay:0 options:UIViewAnimationCurveEaseIn animations:^{
self.view.center = CGPointMake(self.view.center.x, self.view.center.y + offset);
}completion:^(BOOL finished){
}];
}
- (void) keyboardWillHide:(NSNotification *) notification
{
double animationDuration;
animationDuration = [[[notification userInfo] objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];
NSLog(#"Keyboard Will Hide");
[UIView animateWithDuration:animationDuration delay:0 options:UIViewAnimationCurveEaseIn animations:^{
self.view.center = CGPointMake(self.view.center.x, self.view.center.y - offset);
}completion:^(BOOL finished){
}];
}
- (void) keyboardDidShow
{
NSLog(#"Keyboard Did Show");
keyboardIsShowing = YES;
}
- (void) keyboardDidHide
{
NSLog(#"Keyboard Did Hide");
keyboardIsShowing = NO;
}
-(void) willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
if (keyboardIsShowing && UIInterfaceOrientationIsPortrait(toInterfaceOrientation))
{
if (toInterfaceOrientation == UIInterfaceOrientationPortrait && offset == 260)
offset = -260;
else if (toInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown && offset == -260)
offset = 260;
else
return;
[UIView animateWithDuration:.25 delay:0 options:UIViewAnimationCurveEaseIn animations:^{
self.view.center = CGPointMake(self.view.center.x, self.view.center.y + 2* offset);
}completion:^(BOOL finished){
}];
}
}
Unfortunately you will have to manage this yourself. You can interrogate the current orientation and set your y-axis adjustment appropriately.
int movementDistance = -260;
if (UIInterfaceOrientationPortraitUpsideDown == [self interfaceOrientation]) movementDistance = -movementDistance;
You need to set the frame height, or set the contentInset for the view, not the center.

How to scroll view when keyboard is appear in objective c?

I tried this but view is too little to scroll up. How can I scroll more ?
// Call this method somewhere in your view controller setup code.
- (void)registerForKeyboardNotifications
{
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(keyboardWasShown:)
name:UIKeyboardDidShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(keyboardWillBeHidden:)
name:UIKeyboardWillHideNotification object:nil];
}
// Called when the UIKeyboardDidShowNotification is sent.
- (void)keyboardWasShown:(NSNotification*)aNotification
{
NSDictionary* info = [aNotification userInfo];
CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0, 0.0, kbSize.height, 0.0);
scroll.contentInset = contentInsets;
scroll.scrollIndicatorInsets = contentInsets;
// If active text field is hidden by keyboard, scroll it so it's visible
// Your application might not need or want this behavior.
CGRect aRect = self.view.frame;
aRect.size.height -= kbSize.height;
if (!CGRectContainsPoint(aRect, EPostaText.frame.origin) ) {
CGPoint scrollPoint = CGPointMake(0.0, EPostaText.frame.origin.y-(aRect.size.height));
[scroll setContentOffset:scrollPoint animated:YES];
}
}
// Called when the UIKeyboardWillHideNotification is sent
- (void)keyboardWillBeHidden:(NSNotification*)aNotification
{
UIEdgeInsets contentInsets = UIEdgeInsetsZero;
scroll.contentInset = contentInsets;
scroll.scrollIndicatorInsets = contentInsets;
}
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
EPostaText = textField;
}
- (void)textFieldDidEndEditing:(UITextField *)textField
{
EPostaText = nil;
}
The best approach - it is resize the scroll view container to visible area and use this:
- (void)scrollRectToVisible:(CGRect)rect animated:(BOOL)animated
Since I found it, I use TPKeyboardAvoiding
It is working great, and is very easy to setup:
Add a UIScrollView into your view controller's xib
Set the scroll view's class to TPKeyboardAvoidingScrollView (still
in the xib, via the identity inspector)
Place all your controls within that scrollview
Good luck!
This is my code, hope it will help you. It work ok in case you have many textfield
CGPoint contentOffset;
bool isScroll;
- (void)textFieldDidBeginEditing:(UITextField *)textField {
contentOffset = self.myScroll.contentOffset;
CGPoint newOffset;
newOffset.x = contentOffset.x;
newOffset.y = contentOffset.y;
//check push return in keyboar
if(!isScroll){
//180 is height of keyboar
newOffset.y += 180;
isScroll=YES;
}
[self.myScroll setContentOffset:newOffset animated:YES];
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
//reset offset of content
isScroll = NO;
[self.myScroll setContentOffset:contentOffset animated:YES];
[textField endEditing:true];
return true;
}