I have a UITableView inside a UIScrollView. The tableView does not span the length of the view as I have a UITextView at the bottom of the view (to enter messages) so the table ends at the textview. However, The scrollView does span the entire length of the view. When the keyboard comes up, I move the entire view up with it,(so nothing is hidden) but that makes the top cells of the table inaccessible (as in, I don't need to see the cells, but when i try to scroll to them I can't get to them) as it is pushing the whole view up. I am wondering if instead, I can push the scrollview underneath up, in turn having it push the keyboard up with it, and then have the tableview stay in place and scroll to the proper cell (for me in this case it is the most recent message, or last cell in the table). Having the tableview stay in place will allow scrolling access to all the cells in the table. Am I going about this the right way or is there an easier and better way to accomplish this? I'm working on a messaging app, and I want the UI to behave in a similar way to the native messages app. When the keyboard comes up, it brings the last message up above the top of the keyboard, but when the keyboard is visible you can still scroll to the top of the table. I will post my code for the movement of the view.
//call move view method and scroll to last cell in table
-(void) keyboardWasShown:(NSNotification*)aNotification
{
NSLog(#"Keyboard was shown");
[self moveView:[aNotification userInfo] up:YES];
[self.chatTable scrollRectToVisible:CGRectMake(0, self.chatTable.contentSize.height - self.chatTable.bounds.size.height, self.chatTable.bounds.size.width, self.chatTable.bounds.size.height) animated:YES];
}
-(void) keyboardWillHide:(NSNotification*)aNotification
{
NSLog(#"Keyboard will hide");
[self moveView:[aNotification userInfo] up:NO];
}
- (void)moveView:(NSDictionary*)userInfo up:(BOOL)up
{
CGRect keyboardEndFrame;
[[userInfo objectForKey:UIKeyboardFrameEndUserInfoKey]getValue:&keyboardEndFrame];
UIViewAnimationCurve animationCurve;
[[userInfo objectForKey:UIKeyboardAnimationCurveUserInfoKey]
getValue:&animationCurve];
NSTimeInterval animationDuration;
[[userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey]
getValue:&animationDuration];
// Get the correct keyboard size to we slide the right amount.
[UIView beginAnimations:nil context:nil];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationDuration:animationDuration];
[UIView setAnimationCurve:animationCurve];
CGRect keyboardFrame = [self.view convertRect:keyboardEndFrame toView:nil];
int y = keyboardFrame.size.height * (up ? -1 : 1);
self.view.frame = CGRectOffset(self.view.frame, 0, y);
[UIView commitAnimations];
}
What I would do is NOT move the whole view up, but just shrink the scrollView's height by y amount.
So instead of
self.view.frame = CGRectOffset(self.view.frame, 0, y);
you could
CGRect scrlRect = self.myScrollView.frame;
self.myScrollView.frame = CGMakeRect(scrlRect.origin.x, scrlRect.origin.y, scrlRect.size.width, scrlRect.size.height+y);
The first step in dealing with this is modify the insets on the table. This gist would help you ensure that the tableView (or scrollview) scrollable area is no longer covered by the keyboard: https://gist.github.com/TimMedcalf/9505416
It may reveal further issues, but that's always my first port of call.
In your keyboarWasShown method wright below code:
- (void)keyboardWillShow:(NSNotification *)notification
{
NSDictionary *info = [notification userInfo];
CGSize kbSize = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size;
float kbHeight = kbSize.width > kbSize.height ? kbSize.height : kbSize.width;
self.keyboardHeight.constant = kbHeight+5;
}
Now in your textFieldShouldBeginEditing delegate method of UITextField write below code:
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
CGPoint point = [textField.superview convertPoint:textField.frame.origin toView:yourTableViewHere];
NSIndexPath *indexPath = [tblAsset indexPathForRowAtPoint:point];
CGRect tableViewVisibleRect = [tblAsset rectForRowAtIndexPath:indexPath];
if (point.y > (tableViewVisibleRect.size.height - self.keyboardHeight.constant))
{
CGPoint contentOffsetPoint = tblAsset.contentOffset;
contentOffsetPoint.y -= self.keyboardHeight.constant;
CGSize contectSize = tblAsset.contentSize;
contectSize.height += self.keyboardHeight.constant;
[tblAsset setContentSize:contectSize];
[tblAsset setContentOffset:contentOffsetPoint animated:NO];
}
}
Above is from my current project and its working somewhat like what you want. Using this way may help you achive your task.
Related
I have an observed method that trigger when the soft keyboard is shown. It works fine, but for some reason the height of the soft keyboard changes after it has been hidden, then presented for the second time. I can't find a reason for this and there doesn't seem to be anything in the hide-delegate that changes its value. What causes this? I have worked around the problem by storing the height then using it the second time, but I would like to know the cause of this issue.
- (void)keyboardWasShown:(NSNotification*)aNotification
{
NSDictionary* info = [aNotification userInfo];
CGSize keyboardSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
CGRect visibleRect = self.view.frame;
if (_storedKeyboardHeight.size.height == 0) {
_storedKeyboardHeight.size.height = keyboardSize.height;
}
visibleRect.size.height = _storedKeyboardHeight.size.height;
visibleRect.origin.y = self.view.height - visibleRect.size.height;
CGRect rectOfCellInTableView = [self.loginTableView rectForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:1]];
//This changes the second time
NSLog(#"🦁 %f", keyboardSize.height);
//so I store the original value here
NSLog(#"🦁 %#", CGRectCreateDictionaryRepresentation(_storedKeyboardHeight));
if ((rectOfCellInTableView.origin.y + rectOfCellInTableView.size.height) > visibleRect.origin.y){
CGPoint scrollPoint = CGPointMake(0.0, (rectOfCellInTableView.origin.y + rectOfCellInTableView.size.height) - visibleRect.origin.y + 50);
[self.loginTableView setContentOffset:scrollPoint animated:YES];
}
}
First time the height is 291, second time it's 233.
The problem is that you are examining the wrong frame:
UIKeyboardFrameBeginUserInfoKey
When the keyboard is shown, what its frame height is at the beginning of the showing process is of no interest to you. What you want to know is the frame at the end of the showing process:
UIKeyboardFrameEndUserInfoKey
Also, it looks like you are getting notified of the wrong thing. You have not shown what notification you are registered for, but the name of your method, keyboardWasShown, suggests that your are getting notified when the keyboard did show. That's too late; this notification is almost never of any interest. You want to know when the keyboard will show.
I have a screen within an iPhone app that consist of a UITextView. This text view is contained within a UIScrollView. The purpose of the screen is for the user to type in text, and to optionally attach an image to what he is writing. Therefore, the screen also has a UIToolbar with a camera button at the bottom of the screen. The structure of the screen is as follows:
-View
--UIScrollView
---UITextView
--UIToolbar
---UIButton
When the user navigates to this screen, the viewDidAppear method assigns first responder to the uitextview element, so the keyboard shows up, which hides the toolbar and the camera button.
I would like the entire toolbar to re-draw itself right above the keyboard, and to position itself again at the bottom of the screen when the keyboard hides.
I have found related posts on SO (like this one). However, such methods introduce undesired behaviours. For example, implementing the solution in the article above, the toolbar does move with the keyboard, but the UIScrollView gets its frame.origin.y coordinate shifted way above the top of the screen, so it's impossible for the user to see what he is typing.
I have also tried to reset the frame of the toolbar, by adding it as an IBOutlet and using cgrectmake to reposition it. However, after several tries, the toolbar remains stuck at the bottom of the screen and hidden by the keyboard:
- (void) liftMainViewWhenKeybordAppears:(NSNotification*)aNotification{
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDelegate:self];
[UIView setAnimationDuration:0.3];
[UIView setAnimationCurve:<#(UIViewAnimationCurve)#>]
CGRect frame = self.keyboardToolbar.frame;
frame.origin.y = self.keyboardToolbar.frame.origin.y - 280;
self.keyboardToolbar.frame = frame;
[UIView commitAnimations];
}
I have tried several iterations similar to the code above and they all fail at repositioning the toolbar.
So in short, what is the right way to float a toolbar right on top of a keyboard in a screen whose space is completely utilised by a uitextview element?
Thanks to RoryMcKinnel for the pointer. As the article referenced is in Swift, I thought I might paste the solution that worked for be on ObjC
- (void)keyboardWillShowNotification:(NSNotification *)notification{
NSDictionary *userInfo = notification.userInfo;
double animationDuration = [userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];
CGRect keyboardEndFrame = [userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
CGRect convertedKeyboardFrame = [self.view convertRect:keyboardEndFrame fromView:self.view.window];
UIViewAnimationOptions rawAnimationCurve = (UIViewAnimationOptions)[userInfo[UIKeyboardAnimationCurveUserInfoKey] unsignedIntegerValue] << 16;
_toolBarBottomGuide.constant = CGRectGetMaxY(self.view.bounds) - CGRectGetMinY(convertedKeyboardFrame);
[UIView animateWithDuration:animationDuration delay:0.0 options:rawAnimationCurve animations:^{
[self.view layoutIfNeeded];
} completion:nil];
}
Bear in mind, this code did make the toolbar move as required, but the toolbar was not visible at all. It turned out that it was being hidden behind the UIScrollView. This was easily fixed by shifting the order between the scroll view and the toolbar element in the IB hierarchy.
The method above works for the keyboardWillShow event. You'll need to add the corresponding one for when the keyboard hides, like this:
- (void)keyboardWillHideNotification:(NSNotification *)notification{
NSDictionary *userInfo = notification.userInfo;
double animationDuration = [userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];
CGRect keyboardEndFrame = [userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
UIViewAnimationOptions rawAnimationCurve = (UIViewAnimationOptions)[userInfo[UIKeyboardAnimationCurveUserInfoKey] unsignedIntegerValue] << 16;
_toolBarBottomGuide.constant = 0.0f;
[UIView animateWithDuration:animationDuration delay:0.0 options:rawAnimationCurve animations:^{
[self.view layoutIfNeeded];
} completion:nil];
}
In my app I've this login screen:
Above the first test field I've an image, the configuration in Interface Builder is the follow:
Now when I tap on the UITextField they should move up otherwise in iPhone 4/4s the field will be covered by keyboard.
Now I'm using the following code:
-(void)textFieldDidBeginEditing:(UITextField *)textField {
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.35f];
CGRect frameView = self.view.frame;
CGRect frameImage = self.imageViewLogo.frame;
CGRect frameTextFieldUsername = self.textFieldUsername.frame;
CGRect frameTextFieldPassword = self.textFieldPassword.frame;
CGRect frameButtonLogin = self.buttonLogin.frame;
frameView.origin.y = -100;
frameImage.origin.y = -100;
frameTextFieldUsername.origin.y = -100;
frameTextFieldPassword.origin.y = -100;
frameButtonLogin.origin.y = -100;
[self.view setFrame:frameView];
[self.imageViewLogo setFrame:frameImage];
[self.textFieldUsername setFrame:frameTextFieldUsername];
[self.textFieldPassword setFrame:frameTextFieldPassword];
[self.buttonLogin setFrame:frameButtonLogin];
[UIView commitAnimations];
}
When I try to run the app in simulator or on a real device the view scrolls up of 100 but the image, the text fields and button doesn't scrolls up... I thought that the problem depend on the constraints, can you help me to fix this issue?
Thank you
The way in which you are trying to do this will get you in trouble. As you are giving hardcoded values of frame.
Try to use keyboard avoiding library it's the better and safer way of doing it.
-(void)textFieldDidBeginEditing:(UITextField *)textField {
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.35f];
CGRect frameView = self.view.frame;
CGRect frameImage = self.imageViewLogo.frame;
CGRect frameTextFieldUsername = self.textFieldUsername.frame;
CGRect frameTextFieldPassword = self.textFieldPassword.frame;
CGRect frameButtonLogin = self.buttonLogin.frame;
frameView.origin.y = -100;
// no need of changing frames of TextFields inside the View, Just change the y-padding of View
[self.view setFrame:frameView];
[UIView commitAnimations];
}
reduce the y-padding to -120 and check
Hope this helps thanks
If your views are set up to use AutoLayout, changing frame will do nothing.
You should subscribe to UIKeyboardWillChangeFrameNotification and perform your changes there. In case of constraints you would update the constrains constants here and the call -setNeedsUpdateConstraints on the superview.
You have to figure out if the keyboards is appearing or disappearing at all if you want to support external keyboard.
Otherwise you can listen for UIKeyboardWillShowNotification and UIKeyboardWillHideNotification
For auto up the textFields while keyboard comes not need to use the thirdParty Classes.
Create a static tableView and design each textFields and button in each static cells.
TableView automatically handle the auto up textFields while keyboard comes.
Hi the simple and easy trick for doing it is :-
Take outlet of top constraint of username or email id textfield in your class and on textfield did begin editing delegate decrease the constraint's constant value so it will goes up when u select the textfield and at textField did end editing delegate again set constraint's constant value to previous one so it will reset on previous position.
eg:-Assuming you set Top Constraint value in Storyboard is 150
-(void)textFieldDidBeginEditing:(UITextField *)textField {
_topConstraint.constant = 10.00;
[UIView animateWithDuration:0.5f animations:^{
[self.view layoutIfNeeded];
}];
}
-(void)textFieldDidEndEditing:(UITextField *)textField {
_topConstraint.constant = 150.00;
[UIView animateWithDuration:0.5f animations:^{
[self.view layoutIfNeeded];
}];
}
Hope it will help you.
I have a couple UITextFields that will be hidden by the keyboard when it pops up, so I implemented the UIKeyboardWillShowNotification and UIKeyboardWillHideNotification and sent them to their respective methods:
- (void)keyboardWillShow:(NSNotification*)notification
{
CGFloat height = [[notification.userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size.height;
keyBoardHeight = height;
}
- (void)keyboardWillHide:(NSNotification*)notification
{
CGFloat height = [[notification.userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size.height;
keyBoardHeight = height;
}
I then have my textfields delegating to this method:
//TextFieldDelegate
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
float keyboardOffset = [self offsetForKeyboard:textField.frame.origin.y withHeight:textField.frame.size.height];
NSLog(#"%f, %f", keyBoardHeight, keyboardOffset);
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.1];
self.view.frame = [Global rect:self.view.frame withNewY:-[self offsetForKeyboard:textField.frame.origin.y withHeight:textField.frame.size.height]];
[UIView commitAnimations];
}
The code runs properly, but when I run the code, the keyboardWillShow: method runs after the textFieldDidBeginEditing: method. Because of this, keyboardHeight is set to 0 the first time the code runs, and so the offset I calculate is way off.
How can I make the keyboardWillShow: method run before the textFieldDidBeginEditing: method? Is there another delegate method that I can use instead?
Sorry, but no there's no way to do that. You have to do your animations or whatever you do for keyboard handling inside the notification observer.
Here's what Apple recommends and works fine for me in all cases I've encountered upto now.
Apple's Keyboard Guide
For anyone else who is trying to do this, I found a way:
Maintain a weak reference to the currently editing text field:
UITextField* editingTextField;
- (void)textFieldDidBeginEditing:(UITextField*)textField
{
editingTextField = textField;
}
- (void)textFieldDidEndEditing:(UITextField*)textField
{
editingTextField = nil;
}
Then just reference the weak text field while it is open in the keyboardWillShow: method to calculate the appropriate offsets and move the view accordingly.
- (void)keyboardWillShow:(NSNotification*)notification
{
//Use editingTextField's frame here
}
Simply use IQKeyboardManager. It is a single line code for all textfield in any view.Link: https://github.com/hackiftekhar/IQKeyboardManager
I have looked at a number of posts here on scrolling and unhiding a UITextField and believed that the same code should work for a UITextView, but that seems not to be the case. The first issue I encountered was that the sample app I have is an iPad app supporting landscape orientation only. The keyboard size returned from the notification had the height and width of the keyboard reversed.
Next, while I can get the scrollview to scroll the textview, it does not reveal all of it and in fact the amount of the textview that is shown is dependent on where I tap in the textview. It is more like it is scrolling to where the cursor will be which is not what I want.
Here is the code I am using. It was taken from an example, the only real change is that a UITextView is used instead of a UITextField. If the only thing I do is to replace the textview with a textfield it works fine.
- (void)keyboardWasShown:(NSNotification*)aNotification
{
NSDictionary* info = [aNotification userInfo];
CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0, 0.0, kbSize.width, 0.0);
_myscrollview.contentInset = contentInsets;
_myscrollview.scrollIndicatorInsets = contentInsets;
CGRect aRect = self.view.frame;
aRect.size.height -= kbSize.width;
DebugLog(#"textview = %#", _textview);
if (!CGRectContainsPoint(aRect, _textview.frame.origin) ) {
CGPoint scrollPoint = CGPointMake(0.0, _textview.frame.origin.y-kbSize.width);
[_myscrollview setContentOffset:scrollPoint animated:YES];
}
}
If you are just wanting to scroll to the top of your UITextView you can do that by
[textview scrollRectToVisible:CGRectMake(0,0,1,1) animated:YES];
Try messing with the below method. Perhaps set it to the height of the textView. Can you post a screenshot of the issue?
- (void)textViewDidChangeSelection:(UITextView *)textView
{
[textView setSelectedRange:NSMakeRange(0, 0)];
}
Apple has provided the correct method here (check the Listing 4-1). It works for both UITextField and UITextView. We do not need to edit the range or anything else. It should be the scrollview that needs handling. The one marked as 'Answered' may work, but we better follow the creator's method!