zoukankan      html  css  js  c++  java
  • 通过监听键盘,实现对UITextView的内容移动

    视图出现时,增加观察

    - (void)viewWillAppear:(BOOL)animated

    {

        // 增加对键盘的监听

        [[NSNotificationCenter defaultCenter] addObserver:self

         

                                                 selector:@selector(keyboardWasShown:)

         

                                                     name:UIKeyboardWillShowNotification object:nil];

        [[NSNotificationCenter defaultCenter] addObserver:self

         

                                                 selector:@selector(keyboardWasHidden:)

         

                                                     name:UIKeyboardWillHideNotification object:nil];

    }

    #pragma mark -- 与键盘监听相关的方法

    - (void)keyboardWasShown:(NSNotification *)notification

    {

        // 获取键盘高度

        CGRect keyBoardFrame  = [[[notification userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];

        CGFloat keyBoardH      = keyBoardFrame.size.height;

        CGFloat y                = keyBoardH - 120 - self.bottomLayout.constant;

     // 通过改变contentInset的heigh来使textView的字可以跟着移动

        self.contentTextView.contentInset   = UIEdgeInsetsMake(0, 0, y, 0);

    }

    // 键盘消失时 重新改回原来的contentInsert

    - (void)keyboardWasHidden:(NSNotification *)notification

    {

        self.contentTextView.contentInset   = UIEdgeInsetsMake(0, 0, 0, 0);

    }

     

    视图消失时,移除观察

    - (void)viewDidDisappear:(BOOL)animated

    {

        [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];

        [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];

        

    }

  • 相关阅读:
    IEEE 802.11p (WAVE,Wireless Access in the Vehicular Environment)
    齐夫定律, Zipf's law,Zipfian distribution
    信息中心网络 ,Information-centric networking, ICN
    Ubuntu 16.04安装QQ国际版图文详细教程
    IP多媒体子系统(IP Multimedia Subsystem,IMS)
    遗传算法
    再见, 软交换!又一个通信时代的落幕
    矩阵的核、特征向量、值域
    IPv4组播通信原理
    APIPA(Automatic Private IP Addressing,自动专用IP寻址)
  • 原文地址:https://www.cnblogs.com/destiLaugh/p/5821081.html
Copyright © 2011-2022 走看看