【问题标题】:Make bottom of UITableview move up with keyboard [duplicate]使用键盘使 UITableview 的底部向上移动[重复]
【发布时间】:2013-09-09 23:33:36
【问题描述】:

我有一个带有 UITableview 和 UITextView 的应用程序,所以有时键盘会启动。每当键盘打开时,我都无法访问底部的 3-4 个表格单元格,因为它们总是卡在键盘后面。如何让 UITableView 在显示键盘时只占用键盘上方的空间?

【问题讨论】:

    标签: ios objective-c cocoa-touch uitableview


    【解决方案1】:

    试试这个:

    CGRect frame = [tableViewer frame]; //assuming tableViewer is your tableview
    frame.size.height -= 200; //200 may be a bit off, should be height of keyboard
    [tableViewer setFrame:frame]; 
    

    同样的事情,只是在键盘消失时将 -= 更改为 +=。

    【讨论】:

    • 这是唯一对我有用的东西 - 谢谢!一个问题 - 当键盘打开时,通过“打开动画”出现黑色背景,我该如何删除/更改它
    • 桌面视图的黑色背景?
    • 不是表格视图,而是键盘向上移动的“矩形”
    • 哦,我明白了。让 sn-p 在动画之后运行,而不是之前。或者,将视图的背景颜色更改为白色。
    【解决方案2】:

    我通常使用https://github.com/michaeltyson/TPKeyboardAvoiding 这将自动解决您的正常问题 但是如果你想手动做一些编码

    使用表格视图setContentOffsetscrollToRowAtIndexPath

        CGPoint scrollPt = CGPointMake(textFieldRect.origin.x, textFieldRect.origin.y);  
      [_tableView setContentOffset:scrollPt animated:YES];
    
        UITableViewCell *cell = (UITableViewCell*) [[textField superview] superview];
        [_tableView scrollToRowAtIndexPath:[_tableView indexPathForCell:cell] atScrollPosition:UITableViewScrollPositionTop animated:YES];
    

    【讨论】:

      【解决方案3】:

      我遇到了同样的问题,这是我的解决方案。希望对您有所帮助。

      - (void) keyboardWillShow: (NSNotification*) aNotification
      {
          [UIView animateWithDuration: [self keyboardAnimationDurationForNotification: aNotification] animations:^{
              CGSize kbSize = [[[aNotification userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size;
              //change the frame of your talbleiview via kbsize.height
          } completion:^(BOOL finished) {
          }];
      }
      
      - (void) keyboardDidShow: (NSNotification*) aNotification
      {    
          [UIView animateWithDuration: [self keyboardAnimationDurationForNotification: aNotification] animations:^{
              CGSize kbSize = [[[aNotification userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size;
              //change the frame of your talbleiview via kbsize.height
          } completion:^(BOOL finished) {
          }];
      }
      
      - (void) keyboardWillDisappear: (NSNotification*) aNotification
      {
          [UIView animateWithDuration: [self keyboardAnimationDurationForNotification: aNotification] animations:^{
              //restore your tableview
          } completion:^(BOOL finished) {
          }];
      }
      
      - (NSTimeInterval) keyboardAnimationDurationForNotification:(NSNotification*)notification
      {
          NSDictionary* info = [notification userInfo];
          NSValue* value = [info objectForKey: UIKeyboardAnimationDurationUserInfoKey];
          NSTimeInterval duration = 0;
          [value getValue: &duration];
      
          return duration;
      }
      

      添加和删除事件监听器。

      - (void)viewDidLoad
      {
          [super viewDidLoad];
          [[NSNotificationCenter defaultCenter] addObserver: self
                                                   selector: @selector(keyboardWillShow:)
                                                       name: UIKeyboardWillShowNotification object:nil];
          [[NSNotificationCenter defaultCenter] addObserver: self
                                                   selector: @selector(keyboardDidShow:)
                                                       name: UIKeyboardDidShowNotification object:nil];
      
          [[NSNotificationCenter defaultCenter] addObserver: self
                                                   selector: @selector(keyboardWillDisappear:)
                                                       name: UIKeyboardWillHideNotification object:nil];
          }
      - (void) dealloc
      {
          [[NSNotificationCenter defaultCenter] removeObserver: self name: UIKeyboardWillShowNotification object: nil];
          [[NSNotificationCenter defaultCenter] removeObserver: self name: UIKeyboardDidShowNotification object: nil];
          [[NSNotificationCenter defaultCenter] removeObserver: self name: UIKeyboardWillHideNotification object: nil];
      }
      

      在一个键盘动画期间将接收多个事件的 Methion。

      【讨论】:

      • 为什么需要keyboardWillShowkeyboardDidShow
      猜你喜欢
      • 1970-01-01
      • 2019-02-22
      • 2019-06-24
      • 2021-03-03
      • 2021-01-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-08-15
      相关资源
      最近更新 更多