美文网首页
文本控件之UITextField

文本控件之UITextField

作者: 太郎君 | 来源:发表于2016-07-06 23:18 被阅读71次

UITextField控件在登录注册这类填表的页面中常常用到,这里讨论采用的几个体验点

键盘的收起

scrollView滚动时收起键盘
非textField区域被点击时收起键盘

第一个属于滚动处理

_scrollView.keyboardDismissMode = UIScrollViewKeyboardDismissModeInteractive;

第二个属于点击处理

UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onTableViewTapped)];
[_scrollView addGestureRecognizer:tapRecognizer];

- (void)onTableViewTapped
{
    [_idCardNameField resignFirstResponder];
    [_idCardNumberField resignFirstResponder];
    [_mobilePhoneNumberField resignFirstResponder];
    [_mobileVerifyCodeField resignFirstResponder];
}

键盘的Return链

#pragma mark - UITextFieldDelegate
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    if ([_idCardNameField isFirstResponder]) {
        [_idCardNumberField becomeFirstResponder];
    }
    else if([_idCardNumberField isFirstResponder]) {
        [_mobilePhoneNumberField becomeFirstResponder];
    }
    else if([_mobilePhoneNumberField isFirstResponder]) {
        [_mobileVerifyCodeField becomeFirstResponder];
    }
    else if ([_mobileVerifyCodeField isFirstResponder]) {
        [_mobileVerifyCodeField resignFirstResponder];
    }
    
    return YES;
}

键盘的定位

#pragma mark - Keyboard
- (void)keyboardDidShow:(NSNotification *)notification
{
    NSDictionary* info = [notification userInfo];
    CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
    CGFloat kbHeight = kbSize.height + 45;//悬浮bar的高度
    
    UIEdgeInsets contentInsets = UIEdgeInsetsMake(_scrollView.contentInset.top, 0.0, kbHeight, 0.0);
    _scrollView.contentInset = contentInsets;
    _scrollView.scrollIndicatorInsets = contentInsets;
    
    UITextField *firstResponderTextField = nil;
    if ([_idCardNameField isFirstResponder]) {
        firstResponderTextField = _idCardNameField;
    }
    else if ([_idCardNumberField isFirstResponder]) {
        firstResponderTextField = _idCardNumberField;
    }
    else if ([_mobilePhoneNumberField isFirstResponder]) {
        firstResponderTextField = _mobilePhoneNumberField;
    }
    else if ([_mobileVerifyCodeField isFirstResponder]) {
        firstResponderTextField = _mobileVerifyCodeField;
    }
    
    if (firstResponderTextField) {
        CGRect editItemRect = [_scrollView convertRect:firstResponderTextField.bounds fromView:firstResponderTextField];
        
        if (!CGRectIsNull(editItemRect)) {
            editItemRect.size.height += 40;
            [_scrollView scrollRectToVisible:editItemRect animated:YES];
        }
    }
}

- (void)keyboardDidHide:(NSNotification*)notification
{
    UIEdgeInsets contentInsets = UIEdgeInsetsMake(_scrollView.contentInset.top, 0, 0, 0);
    _scrollView.contentInset = contentInsets;
    _scrollView.scrollIndicatorInsets = contentInsets;
}

相关文章

  • 文本控件之UITextField

    UITextField控件在登录注册这类填表的页面中常常用到,这里讨论采用的几个体验点 键盘的收起 scrollV...

  • UITextField

    UITextField 文本输入控件的使用 自定义UITextField样式 响应UITextField的键盘通知

  • UITextField和UIButton

    UITextField UITextField(输入框):是控制文本输入和显示的控件。UITextField也是U...

  • iOS-个人整理04 - UITextField文本输入框

    UITextField--文本框 UITextField是控制文本输入和显示的控件,只能输入单行 遵守代理协议

  • 浅谈UITextField

    [UITextField] 文本输入框 UITextField是常用的文本输入控件,比如我们用的QQ的登录界面,词...

  • UI控件预览

    UI控件预览 UILabel – 文本标签 UIButton – 按钮 UITextField – 文本输入框 U...

  • UITextField

    一、简介 <

  • 文本控件综述

    SDK提供的文本控件 UILabel(继承UIView):无法输入; UITextField(继承UIView):...

  • iOS富文本

    在iOS上能进行文本显示的控件有UILable,UITextField,UITextView。然而这些控件本身对文...

  • 基础控件 UILabel UIButton UITextFie

    UILabel:显示文本的控件 UILabel属性设置 UITextField :输入框,是控制文本输入和显示的控...

网友评论

      本文标题:文本控件之UITextField

      本文链接:https://www.haomeiwen.com/subject/ufakjttx.html