zoukankan      html  css  js  c++  java
  • 在UITableViewController里面实现UITextField与键盘的自适应

    tableview里面对textfield的键盘适应本身就是逻辑实现,利用下面代理函数将textfield的位置移到最优点。

    	//将活跃的textview移动到tableview的中间
    		- (void)textFieldDidBeginEditing:(UITextField*)textField
    		{
    		    UITableViewCell* cell = [self parentCellFor:textField];
    		    if (cell)
    		    {
    		        NSIndexPath* indexPath = [self.tableView indexPathForCell:cell];
    		        [self.tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionMiddle animated:YES];
    		    }
    		}
    
    	//结束textview的编辑时将tableview移动到原来的位置
    		- (void)textFieldDidEndEditing:(UITextField*)textField
    		{
    		    UITableViewCell* cell = [self parentCellFor:textField];
    		    if (cell)
    		    {
    		        NSIndexPath* indexPath = [self.tableView indexPathForCell:cell];
    		        [self textFieldDidEndEditing:textField inRowAtIndexPath:indexPath];
    		        [self.tableView reloadRowsAtIndexPaths:@[ indexPath ] withRowAnimation:UITableViewRowAnimationNone];
    		    }
    		}
    
    	//寻找当前cell在tableview中的位置 
    		- (UITableViewCell*)parentCellFor:(UIView*)view
    		{
    		    if (!view)
    		        return nil;
    		    if ([view isMemberOfClass:[UITableViewCell class]])
    		        return (UITableViewCell*)view;
    		    return [self parentCellFor:view.superview];
    		}
    

    以上方法可以有效的使得tableview中的textview正确的移动位置

  • 相关阅读:
    vue this,$set方法
    表格的拖拽排序功能---应用splice方法
    ES6方法的特性总结
    template functional
    scrollTop, offsetTop, pageYOffset, scrollY 的区别
    Sass @mixin 与 @include
    关于Vue中props的详解
    前端开发工具宝典
    前端js开发常用的60种工具方法
    element ui table表格里面插槽的使用方法
  • 原文地址:https://www.cnblogs.com/cillyfly/p/3818154.html
Copyright © 2011-2022 走看看