• iOS键盘通知弹框使用小结


      项目开发中文本框输入的时候经常会用到键盘弹框遮挡的问题。解决办法就是根据底部键盘弹出的高度动态的改变对应view的位置。这里以多行文本框输入为例,效果图如下。

     

      

      //第一步,注册监听键盘通知

        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardShow:) name:UIKeyboardWillShowNotification object:nil];

        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardHide:) name:UIKeyboardWillHideNotification object:nil];

      //第二步,根据通知调整对应view的位置

      

    #pragma mark - 键盘的通知

    - (void)keyboardShow:(NSNotification *)notify

    {

     //获取键盘弹框的位置信息

        CGRect keyBoardRect = [notify.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];

        CGFloat deltaY = keyBoardRect.size.height/2;

     //添加动画效果,向上偏移键盘高度的二分之一

        [UIView animateWithDuration:[notify.userInfo[UIKeyboardAnimationDurationUserInfoKey] floatValue] animations:^{

            self.bottomView.transform = CGAffineTransformMakeTranslation(0, -deltaY);

        }];

    }

    - (void)keyboardHide:(NSNotification *)note

    {

     //位置还原

        [UIView animateWithDuration:[note.userInfo[UIKeyboardAnimationDurationUserInfoKey] floatValue] animations:^{

            self.bottomView.transform = CGAffineTransformIdentity;

        } completion:^(BOOL finished){

        }];

    }

      //第三步,不用的时候移除掉通知对象 

      

    -(void)dealloc{

        [[NSNotificationCenter defaultCenter]removeObserver:self];

    }

  • 相关阅读:
    Codeforces 959 E Mahmoud and Ehab and the xor-MST
    LightOj 1336 Sigma Function
    某考试 T1 sigfib
    [BOI2007] Sequence
    UOJ 41. 矩阵变换
    [BOI2007] Mokia
    SPOJ 26108 TRENDGCD
    bzoj3545: [ONTAK2010]Peaks
    bzoj3910: 火车
    bzoj1185: [HNOI2007]最小矩形覆盖
  • 原文地址:https://www.cnblogs.com/bigant9527/p/14631084.html
Copyright © 2020-2023  润新知