键盘在UIScrollView中隐藏文本字段。如何根据键盘移动文本字段?
我正在开发一个有15个文本字段的应用程序。所以我用UIScrollView使滚动成为可能。但是当点击那些文本字段进行编辑时,我最后的五个文本字段隐藏在键盘后面。一旦他们处于编辑模式,如何将这些文本字段移动到键盘上方?同样,他们的文本字段在UIScrollView上。不是UIView。键盘在UIScrollView中隐藏文本字段。如何根据键盘移动文本字段?
您需要在scrollView上相应地设置contentSize和contentOffset。所以如果你的最后一个文本框的起始位置是(0,300),你可能会希望你的contentSize是CGSizeMake(0,600)左右,然后将你的contentOffset设置为(0,250)。
我可以知道代码吗?我的意思是,我到目前为止尝试了所有CGSizeMake – dcprog
尝试TPKeyboardAvoidingScrollView。
只要抓住你已经滚动视图,并改变它的类TPKeyboardAvoidingScrollView
,现在当键盘弹出,它会调整它的大小和偏移,这样的字段正确显示。
我建议这样做,因为当你有复杂的布局(很多文本字段和其他组件)时,手工操作可能会非常棘手。这个控制就像它的魅力一样简单!
您可以降低scrollView
Ÿ在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];
}
这里的关键是一些示例代码:
#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];
}
没有什么工作的UIScrollView,这就是为什么我发布了问题 – dcprog
可能重复的[如何使UITextField在键盘存在时向上移动](http://stackoverflow.com/questions/1126726/how-to-make- a-uitextfield-move-up-when-keyboard-is-present) –