为UITextField或UITextView显示键盘时调整界面
-
03-07-2019 - |
题
我有一个表格,每个单元格都包含一个标签和一个文本字段。问题是,当我去编辑最后一行时,键盘隐藏了表格的下半部分,我无法看到正在键入的内容。如何将界面移动到键盘上方,以便查看正在键入的内容?
谢谢, 穆斯塔法
解决方案
您需要为 UIKeyboardDidShowNotification
和 UIKeyboardWillHideNotification
事件注册viewController。当你得到这些,你应该调整你的表格的界限;键盘高度为170像素,因此只需适当缩小或增大表格边界,它应适当调整到键盘。
其他提示
此问题很复杂,具体取决于您的UI方案。在这里,我将讨论UITextField或UITextview驻留在UITableViewCell中的场景。
-
您需要使用NSNotificationCenter来检测UIKeyboardDidShowNotification事件。 请参阅 http://iosdevelopertips.com/user-interface/adjust -textfield隐藏逐keyboard.html 。您需要缩小UITableView框架大小,使其仅占用键盘未覆盖的屏幕区域。
- 如果您点击UITableViewCell,操作系统将自动将单元格定位在UITableView的查看区域内。但是当你点击UITextView或UITableViewCell时它就不会发生,即使它位于UITableViewCell中。
醇>
您需要致电
[myTableView selectRowAtIndexPath:self.indexPath animated:YES scrollPosition:UITableViewScrollPositionBottom];`
以编程方式“点按”细胞。
如果同时实现两个点,您将在键盘正上方看到UITextView / Field位置。请记住,UITableView / Field所在的UITableViewCell不能高于“未覆盖”的UITableViewCell。区域。如果您不是这种情况,则有不同的方法,但我不会在此讨论。
查看此问题: UITableView可编辑文本字段列表
请确保您下方有足够的滚动空间。因为根据我的知识,iPhone会在键盘出现时自动调整并显示聚焦文本框。
删除/注释正在修改rect hight的行似乎可以解决问题。感谢。
修改后的代码:
# define kOFFSET_FOR_KEYBOARD 150.0 // keyboard is 150 pixels height
// Animate the entire view up or down, to prevent the keyboard from covering the author field.
- (void)setViewMovedUp:(BOOL)movedUp
{
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.3];
// Make changes to the view's frame inside the animation block. They will be animated instead
// of taking place immediately.
CGRect rect = self.view.frame;
CGRect textViewRect = self.textViewBeingEdited.frame;
CGRect headerViewRect = self.headerView.frame;
if (movedUp) {
// If moving up, not only decrease the origin but increase the height so the view
// covers the entire screen behind the keyboard.
rect.origin.y -= kOFFSET_FOR_KEYBOARD;
// rect.size.height += kOFFSET_FOR_KEYBOARD;
} else {
// If moving down, not only increase the origin but decrease the height.
rect.origin.y += kOFFSET_FOR_KEYBOARD;
// rect.size.height -= kOFFSET_FOR_KEYBOARD;
}
self.view.frame = rect;
[UIView commitAnimations];
}
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWasShown:)
name:UIKeyboardDidShowNotification
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWasHidden:)
name:UIKeyboardDidHideNotification
object:nil];
keyboardVisible = NO;
- (void)keyboardWasShown:(NSNotification *)aNotification {
if ( keyboardVisible )
return;
if( activeTextField != MoneyCollected)
{
NSDictionary *info = [aNotification userInfo];
NSValue *aValue = [info objectForKey:UIKeyboardBoundsUserInfoKey];
CGSize keyboardSize = [aValue CGRectValue].size;
NSTimeInterval animationDuration = 0.300000011920929;
CGRect frame = self.view.frame;
frame.origin.y -= keyboardSize.height-300;
frame.size.height += keyboardSize.height-50;
[UIView beginAnimations:@"ResizeForKeyboard" context:nil];
[UIView setAnimationDuration:animationDuration];
self.view.frame = frame;
[UIView commitAnimations];
viewMoved = YES;
}
keyboardVisible = YES;
}
- (void)keyboardWasHidden:(NSNotification *)aNotification {
if ( viewMoved )
{
NSDictionary *info = [aNotification userInfo];
NSValue *aValue = [info objectForKey:UIKeyboardBoundsUserInfoKey];
CGSize keyboardSize = [aValue CGRectValue].size;
NSTimeInterval animationDuration = 0.300000011920929;
CGRect frame = self.view.frame;
frame.origin.y += keyboardSize.height-300;
frame.size.height -= keyboardSize.height-50;
[UIView beginAnimations:@"ResizeForKeyboard" context:nil];
[UIView setAnimationDuration:animationDuration];
self.view.frame = frame;
[UIView commitAnimations];
viewMoved = NO;
}
keyboardVisible = NO;
}
tabelview.contentInset = UIEdgeInsetsMake(0, 0, 210, 0);
[tableview scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:your_indexnumber inSection:Your_section]
atScrollPosition:UITableViewScrollPositionMiddle animated:NO];
试试这个我的编码,这对你有帮助