• iOS之取消键盘遮挡


    装逼模式开启—>用UITextFieldDelegate代理来解决

    键盘遮挡最常见的可能就是在登录界面了,无论有多少个textfiled,不论是在VC的任何位置。都有可能造成键盘呼出来时,遮挡输入框。

    如图1


    firstVideo.gif

    两个TextField在VC的下部如何让键盘呼出的时候刚好在你点击TextFiled的下面呢?

    装逼模式代码开始!!!

    首先要写上UITextFieldDelegate代理协议,,,切记!!!

    1:添加文本输入框

    申明两个属性

    @property (nonatomic, weak) UITextField * userNameText;
    
    @property (nonatomic, weak) UITextField * userPwdText;
    UITextField * userNameText = [AutolayoutView autolayoutTextFieldWithPlaceholder:@"账号"];
    
    userNameText.delegate = self;
    
    [self.view addSubview:userNameText];
    
    self.userNameText = userNameText;
    
    UITextField * userPwdText = [AutolayoutView autolayoutTextFieldWithPlaceholder:@"密码"];
    
    userPwdText.delegate = self;
    
    [self.view addSubview:userPwdText];
    
    self.userPwdText = userPwdText;

    2:实现代理方法

    此处主要解决

    // 当前点击textfield的坐标的Y值 + 当前点击textFiled的高度 - (屏幕高度- 键盘高度 - 键盘上tabbar高度)

    // 在这一部 就是了一个 当前textfile的的最大Y值 和 键盘的最全高度的差值,用来计算整个view的偏移量

    - (void)textFieldDidBeginEditing:(UITextField *)textField
    
    {
    
    NSLog(@"textFieldDidBeginEditing");
    
    CGRect frame = textField.frame;
    
    CGFloat heights = self.view.frame.size.height;
    
    // 当前点击textfield的坐标的Y值 + 当前点击textFiled的高度 - (屏幕高度- 键盘高度 - 键盘上tabbar高度)
    
    // 在这一部 就是了一个 当前textfile的的最大Y值 和 键盘的最全高度的差值,用来计算整个view的偏移量
    
    int offset = frame.origin.y + 42- ( heights - 216.0-35.0);//键盘高度216
    
    NSTimeInterval animationDuration = 0.30f;
    
    [UIView beginAnimations:@"ResizeForKeyBoard" context:nil];
    
    [UIView setAnimationDuration:animationDuration];
    
    float width = self.view.frame.size.width;
    
    float height = self.view.frame.size.height;
    
    if(offset > 0)
    
    {
    
    CGRect rect = CGRectMake(0.0f, -offset,width,height);
    
    self.view.frame = rect;
    
    }
    
    [UIView commitAnimations];
    
    }

    3:点击空白处的时候让其回到原来位置

    /**
    
    *  textField 取消选中状态
    
    *
    
    */
    
    -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
    
    {
    
    NSLog(@"touchesBegan");
    
    [self.view endEditing:YES];
    
    NSTimeInterval animationDuration = 0.30f;
    
    [UIView beginAnimations:@"ResizeForKeyboard" context:nil];
    
    [UIView setAnimationDuration:animationDuration];
    
    CGRect rect = CGRectMake(0.0f, 0.0f, self.view.frame.size.width, self.view.frame.size.height);
    
    self.view.frame = rect;
    
    [UIView commitAnimations];
    
    }

    还有点击键盘的return键的时候恢复原状就要在

    - (BOOL)textFieldShouldReturn:(UITextField *)textField;里头处理。

    切记一定要判断当前的textile是否是你点击的self.userNameText了

  • 相关阅读:
    【NOIP2013】 华容道 bfs预处理+bfs
    【NOIP2017】逛公园 最短路+DP
    NOIP上机测试注意事项
    【NOIP2013】货车运输 最大生成树+倍增
    【NOIP2013】 火柴排队 贪心+splay
    【NOIP2013】转圈游戏 快速幂
    【xsy1143】 兔子的数字 搜索
    【xsy1172】 染色 dp
    【NOIP2017】 宝藏 状压dp
    【NOIP2017】列队 splay
  • 原文地址:https://www.cnblogs.com/zhun/p/6405329.html
Copyright © 2020-2023  润新知