我正在开发一个有15个文本字段的应用程序。所以我用UIScrollView使滚动成为可能。但是当点击那些文本字段进行编辑时,我最后的五个文本字段隐藏在键盘后面。一旦他们处于编辑模式,如何将这些文本字段移动到键盘上方?同样,他们的文本字段在UIScrollView上。不是UIView。键盘在UIScrollView中隐藏文本字段。如何根据键盘移动文本字段?
1
A
回答
0
您需要在scrollView上相应地设置contentSize和contentOffset。所以如果你的最后一个文本框的起始位置是(0,300),你可能会希望你的contentSize是CGSizeMake(0,600)左右,然后将你的contentOffset设置为(0,250)。
+0
我可以知道代码吗?我的意思是,我到目前为止尝试了所有CGSizeMake – dcprog
2
尝试TPKeyboardAvoidingScrollView。
只要抓住你已经滚动视图,并改变它的类TPKeyboardAvoidingScrollView
,现在当键盘弹出,它会调整它的大小和偏移,这样的字段正确显示。
我建议这样做,因为当你有复杂的布局(很多文本字段和其他组件)时,手工操作可能会非常棘手。这个控制就像它的魅力一样简单!
0
您可以降低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];
}
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];
}
相关问题
- 1. 的XCode文本字段隐藏键盘
- 2. 移动视图,以便键盘不隐藏文本字段
- 3. 虚拟键盘隐藏字段/文本域/ CONTENTEDITABLE(下面隐藏键盘)
- 4. 如何知道键盘何时隐藏文本字段?
- 5. 如何显示虚拟键盘隐藏的文本字段?
- 6. UIScrollView和仍然键盘隐藏文字
- 7. Android中WebView中的文本字段被键盘隐藏
- 8. 测试键盘在显示时是否隐藏文本字段
- 9. iPhone键盘在UIWebView中隐藏字段
- 10. 键盘隐藏文本框
- 11. 键盘不隐藏时点击第二个文本字段
- 12. 为什么文本字段不能响应隐藏键盘?
- 13. 隐藏键盘时的UITableView和的UIScrollView文本字段以外用户抽头
- 14. 如何为数字文本字段和字母文本字段设置键盘?
- 15. Android隐藏键盘密码字段
- 16. 隐藏键盘密码字段Android
- 17. 键盘隐藏输入字段的WebView
- 18. 黄瓜:如何从键盘输入文本到文本字段?
- 19. 移动TPKeyboardAvoidingScrollView不起作用的键盘文本字段
- 20. 在输入文本框中隐藏移动设备的键盘
- 21. 如何在swift中的键盘上添加文本字段?
- 22. 如何在iPhone中使用键盘获取文本字段sdk
- 23. Swift 3 - iOS 10 - UITextField - 如何自动隐藏键盘和自动移动文本字段
- 24. 如何使文本字段在键盘覆盖时向上移动
- 25. 如何在完成编辑文本字段时关闭键盘
- 26. 如何更改Xcode中文本字段的键盘类型
- 27. 虚拟键盘隐藏编辑文本,从片段设置
- 28. Android键盘隐藏文本输入
- 29. 文本字段中UITable滚动对键盘
- 30. 键盘配件视图隐藏文字
没有什么工作的UIScrollView,这就是为什么我发布了问题 – dcprog
可能重复的[如何使UITextField在键盘存在时向上移动](http://stackoverflow.com/questions/1126726/how-to-make- a-uitextfield-move-up-when-keyboard-is-present) –