您的位置:首页 > 移动开发 > Objective-C

Objective-C-如何监测键盘的高度(iOS)

2016-09-21 13:55 260 查看
//想要监测键盘的高度,我在这里用的是通知的方法

//通知中心

    NSNotificationCenter *center=[NSNotificationCenter
defaultCenter];

    //当键盘将要弹起时候执行方法UIKeyboardWillShowNotification

    [center addObserver:self
selector:@selector(willShow:)
name:UIKeyboardWillShowNotification
object:nil];

    //键盘将要收起时执行方法UIKeyboardWillHideNotification

    [center addObserver:self
selector:@selector(willHide:)
name:UIKeyboardWillHideNotification
object:nil];

//键盘出现的方法

-(void)willShow:(NSNotification *)notice{

    

    //通知里的内容

    NSDictionary *userInfo = [notice
userInfo];

    NSValue *aValue = [userInfo
objectForKey:UIKeyboardFrameEndUserInfoKey];

    CGRect keyboardRect = [aValue
CGRectValue];

    //键盘的高度

    CGFloat keyBoardHeight = keyboardRect.size.height;

    

    //动画

    [UIView
animateWithDuration:0.25
animations:^{

        

    }completion:^(BOOL finished){

    }];

}

//键盘收起的方法

-(void)willHide:(NSNotification *)notice{

    

    //动画

    [UIView
animateWithDuration:0.25
animations:^{

    }completion:^(BOOL finished){

        

    }];

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息