HTML5   发布时间:2022-04-27  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了UITableViewCell中的UITextView平滑自动调整大小显示和隐藏iPad上的键盘,但适用于iPhone大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我已经实现了一个定制的UITableViewCell,其中包含一个UITextView,它可以像用户类型一样自动调整大小,类似于Contacts应用程序中的“Notes”字段。它在我的iPhone上正常工作,但是当我在iPad上进行测试时,我发现了一些非常奇怪的行为:当你到达一行时,键盘隐藏了一个毫秒,然后立即显示出来。我会把它写成一个奇怪的错误,但它实际上导致一些数据丢失,因为如果你打字,它会丢失一个或两个。这是我的代码

代码

// returns the proper height/size for the UITextView based on the String it contains.
// If no String,it assumes a space so that it will always have one line.
- (CGSizE)textViewSize:(UITextView*)textView {
     float fudgeFactor = 16.0;
     CGSize tallerSize = CGSizeMake(textView.frame.size.width-fudgeFactor,kMaxFieldHeight);
     NSString *testString = @" ";
     if ([textView.text length] > 0) {
          testString = textView.text;
     }
     CGSize StringSize = [testString sizeWithFont:textView.font consTrainedToSize:tallerSize lineBreakmode:UILineBreakmodeWordWrap];
     return StringSize;
}

// based on the proper text view size,sets the UITextView's frame
- (void) setTextViewSize:(UITextView*)textView {
     CGSize StringSize = [self textViewSize:textView];
     if (StringSize.height != textView.frame.size.height) {
          [textView setFrame:CGRectMake(textView.frame.origin.x,textView.frame.origin.y,textView.frame.size.width,StringSize.height+10)];  // +10 to allow for the space above the text itself 
     }
}

// as per: http://stackoverflow.com/questions/3749746/uitextview-in-a-uitableviewcell-smooth-auto-resize
- (void)textViewDidChange:(UITextView *)textView {

     [self setTextViewSize:textView]; // set proper text view size
     UIView *contentView = textView.superview;
     // (1) the padding above and below the UITextView should each be 6px,so UITextView's
     // height + 12 should equal the height of the UITableViewCell
     // (2) if they are not equal,then update the height of the UITableViewCell
     if ((textView.frame.size.height + 12.0f) != contentView.frame.size.height) {
         [myTableView beginupdates];
         [myTableView endupdates];

         [contentView setFrame:CGRectMake(0,contentView.frame.size.width,(textView.frame.size.height+12.0f))];
     }
}

- (CGFloat)tableView:(UITableView  *)tableView heightForRowATindexPath:(NSIndexPath  *)indexPath {
     int height;
     UITextView *textView = myTextView;
     [self setTextViewSize:textView];
     height = textView.frame.size.height + 12;
     if (height < 44) { // minimum height of 44
          height = 44;
          [textView setFrame:CGRectMake(textView.frame.origin.x,44-12)];
      }
      return (CGFloat)height;
}

问题

所以,这是发生了什么

>这个代码正好在我的iPhone和iPhone模拟器上正常工作。当我输入文本时,UITextView可以顺利地增长,UITableViewCell也随之增长。
>然而,在iPad模拟器上,它变得很困难。当您在第一行打字时,它工作正常,但是当您到达行尾时,键盘会消失,然后立即重新出现,以便如果用户继续键入应用程序,则会丢失一个或两个字符。
>这里有一些额外的注意事项,我注意到奇怪的行为可能有助于解释:

>另外,我发现删除行[myTableView beginupdates]; [myTableView endupdates];在函数textViewDidChange中:(UITextView *)textView使UITextView正常成长,也不显示和隐藏键盘,但不幸的是,UITableViewCell不会增长到正确的高度。
>更新:these instructions以后,我现在可以阻止文字的奇怪动作;但键盘仍然隐藏和显示,这是非常奇怪的。

有没有人有任何想法,如何让键盘不断显示,而不是隐藏和显示什么时候到iPad上的行尾?

P.S .:我对使用ThreeTwenty不感兴趣。

解决方法

你应该返回NO:

-(BOOL) textViewShouldEndEdiTing:(UITextView *)textView

如果你想随时显示键盘。您应该处理案例,哪个键盘应该被隐藏,通过将YES返回给这个委托函数

编辑:

我再挖一点,当[tableView endupdates]调用时,它基本上做了3件事情:

>禁用在tableView上的用户交互
>更新单元格更改
>在tableView上启用用户交互

SDK(平台)之间的区别在于[UIView setUserInteractionEnabled]方法。由于UITableView不会覆盖setUserInteractionEnabled方法,所以它是从super(UIView)调用的。

调用setUserInteractionEnabled时,寻找一个私有字段_shouldResignFirstResponderWithInteractionDisabled,它返回NO作为认值,所以不会重命名一个响应者(UITextView)

但是在iPad上没有这样的检查AFAIK,所以它在第1步上取消了UITextView,并设置了焦点并使其成为第3步的第一个响应者

基本上,根据SDK文档,textViewShouldEndEdiTing允许您保持焦点,是您唯一的选项。

大佬总结

以上是大佬教程为你收集整理的UITableViewCell中的UITextView平滑自动调整大小显示和隐藏iPad上的键盘,但适用于iPhone全部内容,希望文章能够帮你解决UITableViewCell中的UITextView平滑自动调整大小显示和隐藏iPad上的键盘,但适用于iPhone所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。