ios 自定义cell中有textField,点击textField时,防止键盘挡住textField

自定义cell中的代码:
.h中设置一个代理

@protocol moveDelegate <NSObject>

-(void)CellWith:(NSIndexPath * )index;//将点击的那个textField所在的cell,在tableView中的位置传到控制器界面

@end
@interface QuotedListTableViewCell : UITableViewCell<UITextFieldDelegate>
@property (nonatomic, assign)id<moveDelegate> delegate;
@end

.m文件中在textField被点击时,就进行代理的容错处理

- (void)textFieldDidBeginEditing:(UITextField *)textField {

    if (self.delegate && [self.delegate respondsToSelector:@selector(CellWith:)]) {

        [self.delegate CellWith:_flag];//_flag是在(- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
方法中传过来的indexPath)
    }

}

控制器中代码

//实现代理  将textField所在的cell的indexPath传过来,并由indexPath获取到cell距离屏幕的顶端的距离
-(void)CellWith:(NSIndexPath *)index {


        CGRect rectInTableView = [_quotedTable rectForRowAtIndexPath:index];
        CGRect rectInSuperview = [_quotedTable convertRect:rectInTableView toView:[_quotedTable superview]];

   _rectCell = rectInSuperview;//_rectCell为定义的一个全局的变量//@property (nonatomic, assign)CGRect rectCell;


}
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated]; 
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillhidden: ) name:UIKeyboardWillHideNotification object:nil];

}
//当键盘出现或改变时调用
- (void)keyboardWillShow:(NSNotification *)aNotification
{
    //获取键盘的高度
    NSDictionary *userInfo = [aNotification userInfo];
    NSValue *aValue = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];
    CGRect keyboardRect = [aValue CGRectValue];
    _keyBoardHeight = keyboardRect.size.height;
    NSLog(@"键盘高度%f",_keyBoardHeight);


    float textY = _rectCell.origin.y;//cell距离顶部的距离


    float bottomY = self.view.bounds.size.height - textY;//要编辑的textField离底部的距离

    NSLog(@"cell距离顶部的距离%f",textY);
    NSLog(@"要编辑的textField离底部的距离%f",bottomY);
    NSLog(@"%f",BOUNDS.size.height);


    if (bottomY >= _keyBoardHeight) {

        return;
    }

    _height = _keyBoardHeight-bottomY+40;//要移动的距离

    NSLog(@"要移动的距离%f",_height);

    [UIView animateWithDuration:0.5f animations:^{

        self.view.frame = CGRectMake(0, (0-_height),  self.view.bounds.size.width, self.view.bounds.size.height);



    }];




}
//点击return键盘弹回的时候,view回到原来的位置
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    return YES;

}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
这个问题可能是由于 Dialog 组件中的 TextField 组件的焦点问题导致的。你可以尝试在 Dialog 组件中的 TextField 上添加 autoFocus 属性来解决这个问题,例如: ```jsx <Dialog open={open}> <DialogTitle>Enter your name</DialogTitle> <DialogContent> <TextField autoFocus margin="dense" label="Name" type="text" /> </DialogContent> <DialogActions> <Button onClick={handleClose}>Cancel</Button> <Button onClick={handleSave}>Save</Button> </DialogActions> </Dialog> ``` 如果这个方法不起作用,你可以尝试使用 useLayoutEffect 钩子来延迟渲染 TextField 组件。例如: ```jsx import React, { useState, useLayoutEffect } from 'react'; import { Dialog, DialogTitle, DialogContent, TextField, DialogActions, Button } from '@material-ui/core'; function MyDialog() { const [open, setOpen] = useState(false); useLayoutEffect(() => { if (open) { document.querySelector('.MuiDialog-root .MuiInputBase-input').focus(); } }, [open]); const handleOpen = () => { setOpen(true); }; const handleClose = () => { setOpen(false); }; const handleSave = () => { // your save logic setOpen(false); }; return ( <> <Button onClick={handleOpen}>Open Dialog</Button> <Dialog open={open}> <DialogTitle>Enter your name</DialogTitle> <DialogContent> <TextField margin="dense" label="Name" type="text" /> </DialogContent> <DialogActions> <Button onClick={handleClose}>Cancel</Button> <Button onClick={handleSave}>Save</Button> </DialogActions> </Dialog> </> ); } ``` 这将在 Dialog 组件打开使用 DOM API 来获取 TextField 组件并将焦点设置在输入框上。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值