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}

  • 相关阅读:
    应用程序框架实战三十七:Util最新代码更新说明
    应用程序框架实战三十六:CRUD实战演练介绍
    应用程序框架实战三十五:服务概述
    应用程序框架实战三十四:数据传输对象(DTO)介绍及各类型实体比较
    应用程序框架实战三十三:表现层及ASP.NET MVC介绍(二)
    应用程序框架实战三十:表现层及ASP.NET MVC介绍(一)
    应用程序框架实战二十九:Util Demo介绍
    应用程序框架实战二十八:前端框架决择
    Util应用程序框架公共操作类(十二):Lambda表达式公共操作类(三)
    应用程序框架实战二十六:查询对象
  • 原文地址:https://www.cnblogs.com/ranger-jlu/p/4829692.html
Copyright © 2011-2022 走看看