美文网首页
键盘遮挡

键盘遮挡

作者: BlueBar | 来源:发表于2023-01-09 17:32 被阅读0次
#import "ViewController.h"
@interface ViewController ()
@property(nonatomic ,strong) UITextField * firstResponderTextF;//记录将要编辑的输入框
@end
@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    //监听键盘展示和隐藏的通知
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
}
- (void)dealloc{
    //移除键盘通知监听者
    [[NSNotificationCenter defaultCenter]removeObserver:self name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter]removeObserver:self name:UIKeyboardWillHideNotification object:nil];
}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    if ([self.firstResponderTextF isFirstResponder])[self.firstResponderTextF resignFirstResponder];
}
#pragma maek UITextFieldDelegate
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
    self.firstResponderTextF = textField;//当将要开始编辑的时候,获取当前的textField
    return YES;
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    [textField resignFirstResponder];
    return YES;
}
#pragma mark : UIKeyboardWillShowNotification/UIKeyboardWillHideNotification
- (void)keyboardWillShow:(NSNotification *)notification{
    CGRect rect = [self.firstResponderTextF.superview convertRect:self.firstResponderTextF.frame toView:self.view];//获取相对于self.view的位置
    NSDictionary *userInfo = [notification userInfo];
    NSValue* aValue = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];//获取弹出键盘的fame的value值
    CGRect keyboardRect = [aValue CGRectValue];
    keyboardRect = [self.view convertRect:keyboardRect fromView:self.view.window];//获取键盘相对于self.view的frame ,传window和传nil是一样的
    CGFloat keyboardTop = keyboardRect.origin.y;
    NSNumber * animationDurationValue = [userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey];//获取键盘弹出动画时间值
    NSTimeInterval animationDuration = [animationDurationValue doubleValue];
    if (keyboardTop < CGRectGetMaxY(rect)) {//如果键盘盖住了输入框
        CGFloat gap = keyboardTop - CGRectGetMaxY(rect) - 10;//计算需要网上移动的偏移量(输入框底部离键盘顶部为10的间距)
        __weak typeof(self)weakSelf = self;
        [UIView animateWithDuration:animationDuration animations:^{
            weakSelf.view.frame = CGRectMake(weakSelf.view.frame.origin.x, gap, weakSelf.view.frame.size.width, weakSelf.view.frame.size.height);
        }];
    }
}
- (void)keyboardWillHide:(NSNotification *)notification{
    NSDictionary *userInfo = [notification userInfo];
    NSNumber * animationDurationValue = [userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey];//获取键盘隐藏动画时间值
    NSTimeInterval animationDuration = [animationDurationValue doubleValue];
    if (self.view.frame.origin.y < 0) {//如果有偏移,当影藏键盘的时候就复原
        __weak typeof(self)weakSelf = self;
        [UIView animateWithDuration:animationDuration animations:^{
            weakSelf.view.frame = CGRectMake(weakSelf.view.frame.origin.x, 0, weakSelf.view.frame.size.width, weakSelf.view.frame.size.height);
        }];
    }
}
@end

相关文章

  • 键盘遮挡

  • 键盘遮挡

    链接 : https://forums.xamarin.com/discussion/comment/356942...

  • 键盘遮挡

  • iOS表单键盘遮挡问题

    iOS表单键盘遮挡问题 iOS表单键盘遮挡问题

  • UITableView(键盘遮挡)

    概述 今天要分享的内容是tableView中使用textFiled键盘遮挡问题,正好做了这个就把它写出来了。方法有...

  • 键盘遮挡问题

    不知道大家是否遇见过这种问题,就是在准备输入文字的时候键盘弹出来遮挡住了输入框,导致无法输入.今天给大家讲讲这个问...

  • 键盘遮挡问题

    pods: pod 'IQKeyboardManager' 头文件 pod 'IQKeyboardManager' 搞定

  • 键盘遮挡问题

    总结下之前项目中遇到的键盘遮挡问题,分三种情况: 1.简单的页面,如登录页面等我会用自己封装的KeyBoardMa...

  • 键盘遮挡处理

    键盘弹起遮住输入框这个问题是每个iOS开发者绕不过去的坎,那么到底该如何进行键盘处理?现在有TPKeyboardA...

  • html 键盘遮挡问题

网友评论

      本文标题:键盘遮挡

      本文链接:https://www.haomeiwen.com/subject/teqmcdtx.html