【问题标题】:UITextfield : Text jumps up and down while typingUITextfield :文本在打字时上下跳跃
【发布时间】:2015-07-31 00:08:15
【问题描述】:

我的文本字段输入有问题。在“CommentviewController”中,我有一个可滚动的表格视图来显示以前的 cmets,底部是一个 UItextField,供用户输入评论并发布。问题是:当我尝试在文本字段中键入并切换键盘时,文本会在我键入时上下跳跃。例如,我输入的第一个字符在键盘上方,第二个在键盘下方和后方。有人知道原因吗?测试设备是64GB的itouch5,代码是用Objective C写的,我用的是Xcode 6.3.2。

    <i>`- (void)keyboardWasShown:(NSNotification*)aNotification {
 NSDictionary* info = [aNotification userInfo]; 
CGSize kbSize = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size; 
theKBSize = kbSize; 
UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0, 0.0, kbSize.height - 10, 0.0); 
self.tvComment.contentInset = contentInsets; self.tvComment.scrollIndicatorInsets = contentInsets; 
CGRect uvFrame = _uvComment.frame; 
uvFrame.origin.y = self.view.frame.size.height - kbSize.height - _uvComment.frame.size.height; _uvComment.frame = uvFrame; } `</i>

    <i> `- (void)keyboardWillBeHidden:(NSNotification*)aNotification { 
UIEdgeInsets contentInsets = UIEdgeInsetsZero; 
_tvComment.contentInset = contentInsets;
 _tvComment.scrollIndicatorInsets = contentInsets; 
}`</i>

    <i>`- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string { 
if(string==nil || [string length]<1) { 
[self.btnSend setEnabled: NO];
 }else {
 [self.btnSend setEnabled: YES]; 
}
 return YES; 
}`</i> 

【问题讨论】:

  • 请分享您的 CommentviewController 的代码
  • 没有代码很难给出解决方案。另请注意,问题应该易于理解以便快速回答。
  • - (void)keyboardWasShown:(NSNotification*)aNotification { NSDictionary* info = [aNotification userInfo]; CGSize kbSize = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size; theKBSize = kbSize; UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0, 0.0, kbSize.height - 10, 0.0); self.tvComment.contentInset = contentInsets; self.tvComment.scrollIndicatorInsets = contentInsets; CGRect uvFrame = _uvComment.frame; uvFrame.origin.y = self.view.frame.size.height - kbSize.height - _uvComment.frame.size.height; _uvComment.frame = uvFrame; }
  • // UIKeyboardWillHideNotification 发送时调用 - (void)keyboardWillBeHidden:(NSNotification*)aNotification { UIEdgeInsets contentInsets = UIEdgeInsetsZero; _tvComment.contentInset = contentInsets; _tvComment.scrollIndicatorInsets = contentInsets; } - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string { if(string==nil || [string length]
  • 这里是相关的代码

标签: ios objective-c xcode uitextfield


【解决方案1】:

你在改变tableView的滚动位置吗?

UITableView 将自动滚动,这样UITextField 在选中时不会被键盘隐藏并且键盘会弹出。如果您要更改滚动位置,则可能会干扰自动滚动行为。

【讨论】:

  • 有办法解决吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-05-08
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多