除了顶部文本字段,当我开始键入时,iphone键盘会隐藏所有剩余的文本字段。如何在键盘上方显示活动文本字段,然后在隐藏键盘时将其恢复正常?如何在iPhone键盘显示时显示我的活动文本框?
回答
您必须在视图顶部使用滚动视图,并根据您选择的文本字段实施数学contentOffset设置。 您选择的文本框可以从委托方法获得和偏移的计算可以用文本框框架
手表UIKeyboardWillShowNotification
和UIKeyboardWillHideNotification
来完成。随这些通知提供的NSNotification
对象将包含userInfo字典,其中包含有关键盘完成动画后帧所具有的信息。根据这些通知调整您的布局,以保持您想要被键盘覆盖的东西。
在将所有文本字段置于滚动视图之前,请使用以下代码。
#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];
}
把你UITextField
在UIScrollView
和使用这样的UIScrollView
contentOffSet
属性:
-(BOOL) textFieldShouldBeginEditing:(UITextField *)textField
{
if (textField == txt1) //
{
[txt1 becomesFirstResponder];
scrMainView.contentOffset = CGPointMake(0, 20);
}
else if (textField == txt2) //
{
[txt1 resignFirstResponder];
[txt2 becomesFirstResponder];
scrMainView.contentOffset = CGPointMake(0, 60);
}
//And so on..
return YES;
}
希望,你会得到答案。
谢谢。
什么是scrMainView?提到适当的细节,否则用户不会知道你在暗示什么。 'scrMainView'可能是scollview的权利? – 2013-03-19 09:45:44
是的。 scrMainView是我的滚动视图,所以它会将内容偏移量设置得稍微高一点,所以用户可以轻松地进行交互。 – 2013-03-19 09:47:03
然后请提供那些细节。只需看看我的编辑。它会清楚地说明你的答案。 – 2013-03-19 09:48:39
- 1. 显示iPhone键盘
- 2. 活动开始时显示软键盘
- 3. 如何在保留滚动活动的同时显示键盘?
- 4. 如何在输入文本框时显示键盘
- 5. Windows Phone文本框 - 键盘不显示
- 6. 如何在显示searchController时自动显示键盘
- 7. 显示Alt文本键盘
- 8. 如何在活动开始时显示数字键盘?
- 9. iPhone键盘没有显示
- 10. 时显示键盘
- 11. 在对话框活动中自动显示软键盘
- 12. 如何在iPhone中输入文本时显示键盘UItextfield的上边?
- 13. 在UIWebView中点击文本框时不显示键盘
- 14. 键盘显示时不显示视图?
- 15. 如何以编程方式显示文本框的键盘?
- 16. 如何显示键盘上的标签栏时出现在iphone
- 17. EditText在移动时不显示键盘
- 18. Android:如何在显示软键盘的同时显示活动的上下文菜单
- 19. 如何在软键盘显示时选择文本块
- 20. 显示虚拟键盘时点击WebControl内的文本框
- 21. 如何显示拨号盘活动
- 22. 显示键盘
- 23. 如何在Android活动中显示虚拟键盘
- 24. Android - 键盘显示在活动下面,而不是活动
- 25. 如何在图像具有焦点时显示iPhone键盘?
- 26. 当我的活动开始时,如何显示电话输入软键盘?
- 27. 如何在键盘显示时禁用UIWebView的自动滚动
- 28. 当活动开始时没有文本字段时显示软键盘
- 29. Phonegap如何自动显示键盘
- 30. 如何手动显示ios键盘?
请问你可以在这里粘贴代码吗? – 2013-03-19 07:19:53
弹出虚拟键盘时向上移动整个视图。看到这个。 http://stackoverflow.com/questions/15145341/undock-keyboard-programmatically – 2013-03-19 07:32:57