ios键盘弹起不遮挡输入框

- (void)viewDidAppear:(BOOL)animated{

    

    [super viewDidAppear:animated];

    

    [[NSNotificationCenter defaultCenter] addObserver:self

                                             selector:@selector(keyboardWillShown:)

                                                 name:UIKeyboardWillShowNotification object:nil];

    

    [[NSNotificationCenter defaultCenter] addObserver:self

                                             selector:@selector(keyboardWillBeHidden:)

                                                 name:UIKeyboardWillHideNotification object:nil];


}


- (void)keyboardWillShown:(NSNotification *)aNotification {

    

    BOOL isMessageEdit = [_inputTextField isEditing];

    if(isMessageEdit){

            NSDictionary* info = [aNotification userInfo];

            CGSize keyboardSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;

           float time = [[info objectForKey:UIKeyboardAnimationDurationUserInfoKey] floatValue];

            NSLog(@"keyboardSize.height is %f",keyboardSize.height);

            [UIView animateWithDuration:time animations:^{

                CGRect frame = self.base_baseView.frame;

                frame.origin.y -=  keyboardSize.height;

                self.base_baseView.frame = frame;


            }];

    }

}


- (void)keyboardWillBeHidden:(NSNotification *)aNotification{


    NSDictionary* info = [aNotification userInfo];

    float time = [[info objectForKey:UIKeyboardAnimationDurationUserInfoKey] floatValue];

    [UIView animateWithDuration:time animations:^{

        CGRect frame = self.base_baseView.frame;

        frame.origin.y64;

        self.base_baseView.frame = frame;

    }];

    

}



其中BOOL isMessageEdit = [_inputTextField isEditing];是用来判断是不是_inputTextField这个textfield在编辑状态,然后利用 NSDictionary* info = [aNotification userInfo];来获取键盘的一些信息,我将这个字典打印出来如下:

{

    UIKeyboardAnimationCurveUserInfoKey = 7;

    UIKeyboardAnimationDurationUserInfoKey = "0.25";

    UIKeyboardBoundsUserInfoKey = "NSRect: {{0, 0}, {768, 318}}";

    UIKeyboardCenterBeginUserInfoKey = "NSPoint: {384, 1183}";

    UIKeyboardCenterEndUserInfoKey = "NSPoint: {384, 865}";

    UIKeyboardFrameBeginUserInfoKey = "NSRect: {{0, 1024}, {768, 318}}";

    UIKeyboardFrameEndUserInfoKey = "NSRect: {{0, 706}, {768, 318}}";

}

可以看到键盘的信息,比较常用的UIKeyboardAnimationDurationUserInfoKey键盘弹起的时间,UIKeyboardFrameBeginUserInfoKey可以获取键盘弹出的高度



我是这样做的,想要避免 _inputTextField 被遮挡:
1.在进入页面之前注册一个通知,分别是键盘将要弹起和键盘将要收起的通知:

 [[NSNotificationCenter defaultCenteraddObserver:self

                                             selector:@selector(keyboardWillShown:)

                                                 name:UIKeyboardWillShowNotification object:nil];

    

    [[NSNotificationCenter defaultCenteraddObserver:self

                                             selector:@selector(keyboardWillBeHidden:)

                                                 name:UIKeyboardWillHideNotification object:nil];



2.判断是不是这个文本框在编辑:
BOOL isMessageEdit = [_inputTextField isEditing];
3.获取键盘弹出时间和弹出高度

CGSize keyboardSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKeyCGRectValue].size;

float time = [[info objectForKey:UIKeyboardAnimationDurationUserInfoKeyfloatValue];

4.将view在获取的时间里弹起键盘的高度:

 [UIView animateWithDuration:time animations:^{

                CGRect frame = self.base_baseView.frame;

                frame.origin.y -=  keyboardSize.height;

                self.base_baseView.frame = frame;


            }];

5.在键盘将要收起的通知里,将页面恢复:

[UIView animateWithDuration:time animations:^{

        CGRect frame = self.base_baseView.frame;

        frame.origin.y =  64;

        self.base_baseView.frame = frame;

    }];

我这个带有导航栏,所以从64开始,你的如果没有可以设置y=0哦。

搞定!!



  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在微信小程序开发中,如果在iOS手机上遇到了键盘弹起时会遮挡上面内容的问题,可以通过以下方法解决。 首先,我们需要获取键盘的高度。在小程序中,可以使用wx.getSystemInfoSync()方法来获取系统信息,其中包括键盘的高度。 接下来,我们需要监听键盘弹起和隐藏事件。可以通过监听页面的focus和blur事件来实现。当input框被选中时(focus事件),我们可以将页面整体上移一个键盘高度的距离,以保证输入框不被键盘遮挡。当input框失去焦点时(blur事件),我们将页面恢复到原始位置。 具体实现时,可以在页面的onLoad生命周期函数中调用wx.getSystemInfoSync()方法获取系统信息,并存储键盘的高度。然后在页面的focus和blur事件中分别设置页面的上下边距,以达到上移和恢复的效果。 示例代码如下: ``` // 在页面的onLoad生命周期函数中获取系统信息 onLoad: function() { var that = this; wx.getSystemInfoSync({ success: function(res) { that.setData({ keyboardHeight: res.windowHeight * 0.75 // 假设键盘高度占屏幕高度的3/4 }); } }); }, // input框获取焦点时的事件处理函数 onFocus: function() { this.setData({ marginTop: -this.data.keyboardHeight }); }, // input框失去焦点时的事件处理函数 onBlur: function() { this.setData({ marginTop: 0 }); } ``` 需要注意的是,根据不同手机型号和键盘设置的不同,键盘的高度可能会有所差异,因此上述代码中获取到的键盘高度是一个近似值,可以根据实际情况进行调整。 以上是解决微信小程序开发中iOS手机键盘弹起时会遮挡上面内容的一种方法,希望能对你有所帮助。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值