【问题标题】:Calculating how much to scroll up from keyboard height UITableview计算从键盘高度 UITableview 向上滚动多少
【发布时间】:2014-11-20 09:37:40
【问题描述】:
我有这样的 UIScrollView 和 UITextField。我知道如何在我的 self.view 中显示键盘高度。但是,UITextField 位置不会固定在特定位置,因为用户可以滚动。
只有当它被键盘覆盖时,如何才能向上移动 uitextfield?我需要使用 CGPoint 和 convertPoint 吗?我可以知道怎么做吗?
【问题讨论】:
标签:
ios
objective-c
uitableview
uiscrollview
uitextfield
【解决方案1】:
您应该使用 UITextFieldDelegate。
这是一个如何实现它的示例:
-(void)textFieldDidBeginEditing:(UITextField *)textField {
if(!moved) {
[self animateViewToPosition:self.view directionUP:YES];
moved = YES; }
}
-(void)textFieldDidEndEditing:(UITextField *)textField {
if(moved) {
[self animateViewToPosition:self.view directionUP:NO];
moved = NO;
}
[textField resignFirstResponder];
}
-(BOOL)textFieldShouldReturn:(UITextField *)textField {
[textField resignFirstResponder];
if(moved) {
[self animateViewToPosition:self.view directionUP:NO];
}
moved = NO;
return YES;
}
-(void)animateViewToPosition:(UIView *)viewToMove directionUP:(BOOL)up {
const int movementDistance = -260; // tweak as needed
const float movementDuration = 0.3f; // tweak as needed
int movement = (up ? movementDistance : -movementDistance);
[UIView beginAnimations: @"animateTextField" context: nil];
[UIView setAnimationBeginsFromCurrentState: YES];
[UIView setAnimationDuration: movementDuration];
viewToMove.frame = CGRectOffset(viewToMove.frame, 0, movement);
[UIView commitAnimations];
}
【解决方案2】:
// 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;
}
【解决方案3】:
如果有人想尝试它可能会有所帮助,但它可能不是最好的解决方案
-(void)textFieldDidBeginEditing:(UITextField *)textField
{
CGRect actualframe = [textField convertRect:[textField bounds] toView:self.view];
if ((actualframe.origin.y) >= (self.view.frame.size.height-keyboardheignt))
{
CGRect scrollfrm = [textField convertRect:[textField bounds] toView:tableview];
[tableview setContentOffset:CGPointMake(0, scrollfrm.origin.y) animated:YES];//60
}
}
如果文本字段是表格的最后一行,则必须使用内容插入