Reputation: 113
I have a table view and a view that has a text field and multiple buttons in it. The view is at the bottom of the screen, under the UITableView (as a standart message panel). When the user taps text field, keyboard appears, the view including text field moves up. I also change the size of table view, shorten it. Everything works as I wanted.
However, when I go to another view controller from this one (for looking picture) and come back, this system is not working as it should do. Keyboard appears but text field does not go up. Sometimes there is a blank (as there is view) but at the end, there's not text field or button.
My codes for this:
- (void) keyboardWasShown:(NSNotification *)notification {
// get keyboard height
NSDictionary* keyboardInfo = [notification userInfo];
NSValue* keyboardFrameEnd = [keyboardInfo valueForKey:UIKeyboardFrameEndUserInfoKey];
CGRect keyboardRect = [keyboardFrameEnd CGRectValue];
self.keyboardHeight = keyboardRect.size.height;
// shorten tableview. so all of it will be visible
[self.chatTableViewBottomConstraint setConstant:self.tableViewBottomToLayoutConstraint + self.keyboardHeight-54.0];
[self.chatTableView setContentOffset:CGPointMake(0, CGFLOAT_MAX)]; // always the last item will be visible
// move textfield up
[self.textFieldViewBottomConstraint setConstant:+self.keyboardHeight-54.0];
[self.view bringSubviewToFront:self.textFieldView];
}
- (void) keyboardWillBeHidden:(NSNotification *)notification {
// restore tableview height and textfield position
[self.textFieldViewBottomConstraint setConstant:0.0f];
[self.chatTableViewBottomConstraint setConstant:self.tableViewBottomToLayoutConstraint];
}
Using a footer at the bottom of UITableView would be good for my target but when the table view is empty, this footer goes to up. This problem is not nice.
Then, have i missed anything? Why my system does not work as it should do?
Thanks!
Answer: The problem is not my keyboard methods. I found the reason, it's about tab bar which i hide in other view controller. I set it not hidden and everything worked fine.
In other words, problem is not about keyboard nor text field.
Upvotes: 0
Views: 216
Reputation: 1555
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.
Upvotes: 0
Reputation: 11567
Move your view upwards using the code below.
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField {
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidEndEditing:) name:UIKeyboardDidHideNotification object:nil];
[self.view endEditing:YES];
return YES;
}
-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidStartEditing:) name:UIKeyboardDidShowNotification object:nil];
return YES;
}
- (void)keyboardDidStartEditing:(NSNotification *)notification
{
[self.view setFrame:CGRectMake(0,-110,320,460)];
}
-(void)keyboardDidEndEditing:(NSNotification *)notification
{
[self.view setFrame:CGRectMake(0,0,320,460)];
}
Hope it works.
Upvotes: 1
Reputation: 9923
After setting constraint's constant, you have to call [self.view layoutIfNeeded]
for it to take effect
Upvotes: 0