Cocoa touch 为UITextField或UITextView显示键盘时调整界面
我有一个表格,每个单元格包含一个标签和一个文本字段。问题是,当我编辑最后一行时,键盘会隐藏表格的下部,我看不到正在键入的内容。如何将界面移到键盘上方,以便查看正在键入的内容 谢谢,Cocoa touch 为UITextField或UITextView显示键盘时调整界面,cocoa-touch,Cocoa Touch,我有一个表格,每个单元格包含一个标签和一个文本字段。问题是,当我编辑最后一行时,键盘会隐藏表格的下部,我看不到正在键入的内容。如何将界面移到键盘上方,以便查看正在键入的内容 谢谢, Mustafa只要确保下面有足够的滚动空间即可。因为据我所知,iPhone会在其键盘出现时自动调整并显示聚焦文本框。检查此问题:您需要为UIKeyboardDidShowNotification和UIKeyboardWillHideNotification事件注册viewController。当你得到这些,你应该调整
Mustafa只要确保下面有足够的滚动空间即可。因为据我所知,iPhone会在其键盘出现时自动调整并显示聚焦文本框。检查此问题:您需要为
UIKeyboardDidShowNotification
和UIKeyboardWillHideNotification
事件注册viewController。当你得到这些,你应该调整你的表的边界;键盘的高度为170像素,因此只需适当地缩小或扩大表格边界,并应适当地调整键盘。删除/注释修改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];
}此问题很复杂,具体取决于您的UI场景。这里我将讨论一个场景,其中UITextField或UITextview位于UITableViewCell中
[myTableView selectRowAtIndexPath:self.indexPath animated:YES scrollPosition:UITableViewScrollPositionBottom];`
以编程方式“点击”单元格
如果实现这两点,您将看到UITextView/字段位置就在键盘上方。请记住,UITableView/字段所在的UITableViewCell不能高于“未覆盖”区域。如果你不是这样,有一种不同的方法,但我不会在这里讨论
[[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;
}
试试这个我的编码这对u有帮助那么从编程角度来说,我怎么知道它有170像素高呢?亲爱的@Yar,看看这里的答案:0.300000011920929-你是认真的吗?你为什么要用这个神奇的数字?我发现键盘动画需要250毫秒(至少在iPhone 6.0模拟器中)。我刚刚发现你可以在
UIKeyboardWillShowNotification
-回调中用[[notification userInfo]objectForKey:UIKeyboardAnimationDurationUserInfoKey]以编程方式检查持续时间
。
tabelview.contentInset = UIEdgeInsetsMake(0, 0, 210, 0);
[tableview scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:your_indexnumber inSection:Your_section]
atScrollPosition:UITableViewScrollPositionMiddle animated:NO];