关于键盘出现与隐藏时调整UITextField的显示位置问题

简介:

1、首先在ViewDidLoad里面添加注册键盘隐藏与出现的通知:

    ///< 注册通知,以便在键盘将要出现时,调整页面
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(onKeyboardWillShow:)
                                                 name:UIKeyboardWillShowNotification
                                               object:nil];
    ///< 注册通知,以便在键盘将要隐藏时,恢复页面
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(onKeyboardWillHide:)
                                                 name:UIKeyboardWillHideNotification
                                               object:nil];

2、在viewWillDisappear里移除对键盘隐藏与出现的通知:

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    
    /// 注意:在此处需要移除通知,否则容易照成崩溃
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
    return;
}

3、实现键盘出现、隐藏时的通知回调:

#pragma mark - UIKeyboardWillShowNotification 通知
- (void)onKeyboardWillShow:(NSNotification *)notification {
    CGRect frame = self.view.frame;
    if (frame.origin.y < 0) {
        return;
    }
    // 获取键盘的高度
    CGFloat keyboardHeight = [self keyboardHeightWithKeyboardNotification:notification];
    frame.origin.y -= (keyboardHeight - 20);
    [UIView animateWithDuration:0.35 animations:^{
        self.view.frame = frame;
    }];
    return;
}

#pragma mark - UIKeyboardWillHideNotification 通知
- (void)onKeyboardWillHide:(NSNotification *)notification {
    CGRect frame = self.view.frame;
    if (frame.origin.y > 0) {
        return;
    }
    // 获取键盘的高度
    CGFloat keyboardHeight = [self keyboardHeightWithKeyboardNotification:notification];
    frame.origin.y += (keyboardHeight - 20);
    [UIView animateWithDuration:0.35 animations:^{
        self.view.frame = frame;
    }];
    return;
}

/// 获取键盘高度
- (CGFloat)keyboardHeightWithKeyboardNotification:(NSNotification *)notification {
    NSDictionary *info = notification.userInfo;
    NSValue *value = [info objectForKey:UIKeyboardFrameBeginUserInfoKey];
    CGSize keyboardSize = [value CGRectValue].size;
    CGFloat keyboardHeight = keyboardSize.height;
    
    return keyboardHeight;
}


目录
相关文章
|
2月前
|
定位技术 iOS开发
在地图页面,自动布局控件开始是隐藏或在屏幕外需要正常显示时再为正常的显示状态的,需要在显示之前加入
在地图页面,自动布局控件开始是隐藏或在屏幕外需要正常显示时再为正常的显示状态的,需要在显示之前加入
27 0
|
2月前
如何在屏幕的任意位置拖拽控制图片的移动?
如何在屏幕的任意位置拖拽控制图片的移动?
56 1
|
2月前
|
iOS开发
ios中,输入框获得焦点时,页面输入框被遮盖,定位的元素位置错乱
ios中,输入框获得焦点时,页面输入框被遮盖,定位的元素位置错乱
55 1
布局之悬浮显示更多文本并增加箭头指示效果
布局之悬浮显示更多文本并增加箭头指示效果
101 0
布局之悬浮显示更多文本并增加箭头指示效果
|
JavaScript 前端开发
实现 input无内容是缩小居中显示,有内容,有焦点时变长显示
前两天做了一个搜索中间页,有个 input 的效果挺有意思,准备分享一下。
161 0
实现 input无内容是缩小居中显示,有内容,有焦点时变长显示
|
C#
WPF 获取鼠标屏幕位置、窗口位置、控件位置
原文:WPF 获取鼠标屏幕位置、窗口位置、控件位置 public struct POINT { public int X; public int Y; ...
1851 0
|
开发者
解决 Popup 位置不随窗口移动更新的问题
原文:解决 Popup 位置不随窗口移动更新的问题 Popup弹出后,因业务需求设置了StaysOpen=true后,移动窗口位置或者改变窗口大小,Popup的位置不会更新。 如何更新位置? 获取当前Popup的Target绑定UserControl所在窗口,位置刷新时,时时更新Popup的位置即可。
1040 0
|
JavaScript 前端开发 .NET