zoukankan      html  css  js  c++  java
  • 键盘弹起收起时不遮挡处理

    view初始化时增加通知:

    {code}

       //增加监听,当键盘出现或改变时收出消息

            [[NSNotificationCenter defaultCenter] addObserver:self

                                                     selector:@selector(keyboardWillShow:)

                                                         name:UIKeyboardWillShowNotification

                                                       object:nil];

            

            //增加监听,当键退出时收出消息

            [[NSNotificationCenter defaultCenter] addObserver:self

                                                     selector:@selector(keyboardWillHide:)

                                                         name:UIKeyboardWillHideNotification

                                                       object:nil];

    {code}

    键盘弹起和收起时触发的动作

    {code}

    - (void)keyboardWillShow:(NSNotification *)notification {

        if (!_highlightedTextField) {// 当前焦点TextField

            return;

        }

        

        UIView *view = [self superview];

        while (![view isKindOfClass:[UIScrollView class]] &&

               [view superview]) {

            view = [view superview];

        }

        if (![view isKindOfClass:[UIScrollView class]]) {

            return;

        }

        

        UIScrollView *scrollView = (UIScrollView *)view;

        

        CGSize kbSize = [[notification.userInfo objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;

        CGFloat keyboardHeight = kbSize.height;

        

        UITextField *textField = _highlightedTextField;

        

        if ([UIApplication sharedApplication].windows.count == 0) {

            return;

        }

        

        UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0, 0.0, keyboardHeight, 0.0);

        scrollView.contentInset = contentInsets;

        scrollView.scrollIndicatorInsets = contentInsets;

        

        UIWindow *window = [UIApplication sharedApplication].windows[0];

        CGRect textRect = [scrollView convertRect:textField.bounds fromView:textField];

        CGRect scrollRect = [window convertRect:scrollView.bounds fromView:scrollView];

        

        CGFloat scrollBottomMargin = window.height - scrollRect.origin.y - scrollRect.size.height;

        

        CGFloat offset = textRect.origin.y + textRect.size.height - scrollView.contentOffset.y - scrollBottomMargin - (scrollView.height - keyboardHeight);

        

        if (offset > 0) {

            CGPoint scrollPoint = CGPointMake(0.0, scrollView.contentOffset.y + offset);

            [scrollView setContentOffset:scrollPoint animated:YES];

        }

    }

     

    - (void)keyboardWillHide:(NSNotification *)notification{

        UIView *view = [self superview];

        

        while (![view isKindOfClass:[UIScrollView class]] &&

               [view superview]) {

            view = [view superview];

        }

        if (![view isKindOfClass:[UIScrollView class]]) {

            return;

        }

        

        UIScrollView *scrollView = (UIScrollView *)view;

        

        UIEdgeInsets contentInsets = UIEdgeInsetsZero;

        scrollView.contentInset = contentInsets;

        scrollView.scrollIndicatorInsets = contentInsets;

    }

    {code}

  • 相关阅读:
    python_函数_文件
    Day_2_Python_str_list_dict的使用
    Day_1_Python_循环和格式化
    influxdb2.0版本部署+自启
    格式化Java内存工具JOL输出
    卷心菜的屯币日记
    influxDB时序数据库2.0FLUX查询语法使用记录
    两种转换2021-01-01T00:00:00Z为2021-01-01 00:00:00时间格式的方式(UTC时间转为yyyy-MM-dd HH:mm:ss)
    ThreadLocal的用处
    CentOS7使用ISO镜像文件作为离线Yum源
  • 原文地址:https://www.cnblogs.com/ranger-jlu/p/4829692.html
Copyright © 2011-2022 走看看