我正在开发一个有 15 个文本字段的应用程序。所以我使用 UIScrollView 使滚动成为可能。但是当单击这些文本字段进行编辑时,我的最后五个文本字段隐藏在键盘后面。一旦它们处于编辑模式,如何将这些文本字段移动到键盘上方?同样,它们的文本字段位于 UIScrollView 上。不是 UIView。
问问题
3800 次
4 回答
2
尝试TPKeyboardAvoidingScrollView。
只需抓住您已有的滚动视图并将其类更改为TPKeyboardAvoidingScrollView
,现在当键盘弹出时,它将调整其大小和偏移量,以便正确显示字段。
我建议这样做,因为当您有复杂的布局(许多文本字段和其他组件)时,手动执行此操作可能会很棘手。这个控件非常简单,并且工作起来就像一个魅力!
于 2012-10-03T17:10:34.737 回答
1
这是一些示例代码:
#define kOFFSET_FOR_KEYBOARD 80.0
-(void)keyboardWillShow {
// Animate the current view out of the way
if (self.view.frame.origin.y >= 0)
{
[self setViewMovedUp:YES];
}
else if (self.view.frame.origin.y < 0)
{
[self setViewMovedUp:NO];
}
}
-(void)keyboardWillHide {
if (self.view.frame.origin.y >= 0)
{
[self setViewMovedUp:YES];
}
else if (self.view.frame.origin.y < 0)
{
[self setViewMovedUp:NO];
}
}
-(void)textFieldDidBeginEditing:(UITextField *)sender
{
if ([sender isEqual:mailTf])
{
//move the main view, so that the keyboard does not hide it.
if (self.view.frame.origin.y >= 0)
{
[self setViewMovedUp:YES];
}
}
}
//method to move the view up/down whenever the keyboard is shown/dismissed
-(void)setViewMovedUp:(BOOL)movedUp
{
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.3]; // if you want to slide up the view
CGRect rect = self.view.frame;
if (movedUp)
{
// 1. move the view's origin up so that the text field that will be hidden come above the keyboard
// 2. increase the size of the view so that the area behind the keyboard is covered up.
rect.origin.y -= kOFFSET_FOR_KEYBOARD;
rect.size.height += kOFFSET_FOR_KEYBOARD;
}
else
{
// revert back to the normal state.
rect.origin.y += kOFFSET_FOR_KEYBOARD;
rect.size.height -= kOFFSET_FOR_KEYBOARD;
}
self.view.frame = rect;
[UIView commitAnimations];
}
- (void)viewWillAppear:(BOOL)animated
{
// register for keyboard notifications
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillShow)
name:UIKeyboardWillShowNotification
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillHide)
name:UIKeyboardWillHideNotification
object:nil];
}
- (void)viewWillDisappear:(BOOL)animated
{
// unregister for keyboard notifications while not visible.
[[NSNotificationCenter defaultCenter] removeObserver:self
name:UIKeyboardWillShowNotification
object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self
name:UIKeyboardWillHideNotification
object:nil];
}
于 2012-10-03T17:56:07.960 回答
0
你可以减少你的scrollView
Y坐标textFieldShouldBeginEditing:
-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
if(textField == textField11 || textField == textField12 || textField == textField13 || textField == textField14 || textField == textField15)
{
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.3];
//set Y according to keyBoard height
[scrollView setFrame:CGRectMake(0.0,-220.0,320.0,460.0)];
[UIView commitAnimations];
}
}
并按键盘的返回键设置scrollView
框架
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.3];
[scrollView setFrame:CGRectMake(0.0,0.0,320.0,460.0)];
[UIView commitAnimations];
}
于 2012-10-03T17:19:53.813 回答
0
您需要在 scrollView 上相应地设置 contentSize 和 contentOffset。因此,如果您的最后一个文本框的原点为 (0,300),您可能希望 contentSize 为 CGSizeMake(0, 600) 左右,然后将 contentOffset 设置为 (0, 250)。
于 2012-10-03T17:06:00.473 回答