【问题标题】:ipad keyboard hides uitextViewipad键盘隐藏uitextView
【发布时间】:2012-07-16 04:53:34
【问题描述】:

我在 ipad 中有很长的表格,有 uitextfields 和 uitextviews。当键盘使用键盘通知隐藏 uitextfield 时,我可以向上滚动。但是当 uitextview 变为活动状态并且它位于键盘下方时,它只会滑动到足以让我看到闪烁的光标。这是正常行为吗?如果不是,我如何向上滚动整个 uitextview,以便在编辑时可以看到整个 uitextview?

这是代码..

-(void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:YES];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWasShown:) name: UIKeyboardDidShowNotification object:nil];

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillBeHidden:) name:UIKeyboardWillHideNotification object:nil];
}

#pragma mark Keyboard Events

-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    return NO;
}

#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
-(void)keyboardWasShown:(NSNotification *)aNotification
{

if (displayKeyboard==YES) {
    return;
}
NSDictionary* info = [aNotification userInfo];
NSValue* aValue = [info objectForKey:UIKeyboardBoundsUserInfoKey];
//NSValue* aValue = [info objectForKey:UIKeyboardFrameBeginUserInfoKey];
CGSize keyboardSize = [aValue CGRectValue].size;


offset = _scrollView.contentOffset;

CGRect viewFrame = _scrollView.frame;

viewFrame.size.height -= keyboardSize.height-tabbarHt;
_scrollView.frame = viewFrame;

CGRect textFieldRect = [activeField frame];
textFieldRect.origin.y += 10;
[_scrollView scrollRectToVisible: textFieldRect animated:YES];
displayKeyboard = YES;

}

-(void)keyboardWillBeHidden:(NSNotification *)aNotification
{

if (!displayKeyboard) {
    return; 
}

_scrollView.frame = CGRectMake(0, 0, 1024, 655);
_scrollView.contentOffset =offset;
displayKeyboard = NO;
}

-(BOOL) textFieldShouldBeginEditing:(UITextField*)textField {
activeField = textField;
return YES;
}

【问题讨论】:

  • 你必须在 UITextViewDelegate 方法中设置向上滚动的方法
  • @CocoaMatters 你能告诉我该怎么做吗?
  • UITextField 做得怎么样?
  • @CocoaMatters 使用键盘 didshow 和 didhide 通知..并在这些通知上调用相应的方法,并在这些方法中调整滚动视图大小,如果我的文本字段被隐藏,我向上滚动它。相同的代码部分适用于 textview。它向上滚动但不完全。只有一行 textview 是可见的。
  • 你能贴出这段代码吗?

标签: ipad uitextview ios5.1


【解决方案1】:

给你的 UITextView 委托

CGRect activeField;

- (void)textViewDidBeginEditing:(UITextView *)textView
{
    activeField = textView.frame;
}

-(BOOL) textFieldShouldBeginEditing:(UITextField*)textField {
   activeField = textField.frame;
   return YES;
}

尝试使用此代码。每次您只给出 textField 矩形大小。当您调用文本视图时,您必须传递 textview 框架大小

【讨论】:

  • 但是 activeField 是一个 UItextField 对象...我应该为 Uitextview 制作一个类似的对象吗?
  • 做一件事直接从 UItextField 和 UItextView 传递框架而不是创建新对象
  • 是的,我想通了。感谢您的帮助。
猜你喜欢
  • 1970-01-01
  • 2015-02-22
  • 2011-07-30
  • 1970-01-01
  • 1970-01-01
  • 2011-06-16
  • 1970-01-01
  • 2012-04-19
  • 1970-01-01
相关资源
最近更新 更多