菜鸟教程小白 发表于 2022-12-11 18:04:20

ios - 当键盘出现iOS时向上滚动tableview行


                                            <p><p>当键盘出现时,我的应用程序的一些 ui 组件从底部隐藏,实际上它是 <code>tableView 的</code>最后一行。</p>

<p>我正在使用 <code>NSNotificationCenter</code> 来通知键盘何时出现和消失。</p>

<p>这是我的代码:</p>

<pre><code>[ addObserver:self selector:@selector(keyboardOnScreen:) name:UIKeyboardDidShowNotification object:nil];
</code></pre>

<hr/>

<pre><code>-(void)keyboardOnScreen:(NSNotification*)notification{
    NSDictionary* info = ;
    CGSize kbSize = [ CGRectValue].size;
    CGRect screen = self.view.frame;      
    screen.size.height -= kbSize.height;
    float HeightOfToolbar =_inputbar.frame.size.height; //TOOLBAR
    float yPoint = screen.size.height-HeightOfToolbar;
    CGPoint scrollPt = CGPointMake(0, yPoint);
    ;
}
</code></pre>

<p>但是,tableview 向上滚动但不在正确的行,即键盘上方的最后一行。
有时它会滚动到隐藏最后一行的倒数第二行,或者更多。
它与 scrollPt 值有关!</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>使用此方法出现键盘</p>

<pre><code>- (void)keyboardWasShown:(NSNotification *)aNotification {
      NSDictionary* info = ;
      CGSize Size = [ CGRectValue].size;

      UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0, 0.0, Size.height - HeightOfToolbar, 0.0);//Set height according to key board height
      self.tableView.contentInset = contentInsets;
      self.tableView.scrollIndicatorInsets = contentInsets;
      ;
      }
</code></pre>

<p><strong>隐藏键盘</strong> </p>

<pre><code> - (void)keyboardWasHidden:(NSNotification *)aNotification
    {

    self.tableView.contentInset = UIEdgeInsetsZero;
    self.tableView.scrollIndicatorInsets = UIEdgeInsetsZero;
    }
</code></pre></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 当键盘出现iOS时向上滚动tableview行,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/40607988/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/40607988/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 当键盘出现iOS时向上滚动tableview行