keyboard 监听 改变高度

本文介绍了如何在iOS应用中适配键盘的变化,包括监听键盘显示、隐藏及尺寸改变等事件,并通过Swift与Objective-C两种语言实现了键盘弹出时界面元素的平滑调整。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Swift:

1.注册通知  监听

       NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardChangeFrame:", name: UIKeyboardDidChangeFrameNotification, object: nil);

        NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardWillShow:", name: UIKeyboardWillShowNotification, object: nil);

        NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardWillHidden:", name: UIKeyboardWillHideNotification, object: nil)

2.实现三个方法,change如果是数字键盘,用不到可为里面不写东西

    func keyboardWillShow(aNotification:NSNotification){

        keyboardStatus = KeyBoardStatus.toShow

        let moveFrame = CGRectMake(0, WXDevice.height - colorSizeVMoveHeight - CGRectGetHeight(self.frame), WXDevice.width, CGRectGetHeight(self.frame))

        

        UIView.animateWithDuration(0.25, animations: { () -> Void in

            self.frame = moveFrame

            }) { (animated) -> Void in

                

        }

    }


    func keyboardWillHidden(notification:NSNotification){

        UIKeyboardAnimationDurationUserInfoKey

        

        let moveFrame = CGRectMake(0, WXDevice.height - CGRectGetHeight(self.frame), WXDevice.width, CGRectGetHeight(self.frame))

        UIView.animateWithDuration(0.25, animations: { () -> Void in

            self.frame = moveFrame

            }) { (animated) -> Void in

        }

        if notification.userInfo != nil{

            keyboardStatus = KeyBoardStatus.toHidden

            

            let keyboardInfo: NSDictionary = notification.userInfo!;

            if let interval:Double = keyboardInfo.valueForKey(UIKeyboardAnimationDurationUserInfoKey) as? Double

            {

                UIView.animateWithDuration(interval, animations: { () -> Void in

                    self.contentScrollV.contentSize = CGSizeMake(WXDevice.width, self.preContentSize.height)

                    let yOffset = (self.preContentSize.height - CGRectGetHeight(self.contentScrollV.frame) >= 0) ?

                        (self.preContentSize.height - CGRectGetHeight(self.contentScrollV.frame)) : 0

                    self.contentScrollV.contentOffset = CGPointMake(0, yOffset)

                })

            }

        }

    }

    

    func keyboardChangeFrame(aNotification:NSNotification){

        if aNotification.userInfo != nil && keyboardStatus == KeyBoardStatus.toShow{

            let keyboardInfo: NSDictionary = aNotification.userInfo!;

            let keyboardFrame: NSValue = keyboardInfo.valueForKey(UIKeyboardFrameEndUserInfoKey) as! NSValue;

            let _frame: CGRect = keyboardFrame.CGRectValue();

            

            var offset = (WXDevice.height - _frame.origin.y) - (CGRectGetHeight(sureBtn.frame) + 12)

            offset = offset - colorSizeVMoveHeight

            if offset > 0{

                contentScrollV.contentSize = CGSizeMake(WXDevice.width, preContentSize.height + offset)

                contentScrollV.scrollRectToVisible(CGRectMake(0, contentScrollV.contentSize.height - 1/WXDevice.scale, WXDevice.width, 1/WXDevice.scale), animated: false)

            }

        }

    }

    

3.移除

deinit {

        

        NSNotificationCenter.defaultCenter().removeObserver(self)

        DPrintln(__FUNCTION__)

    }


OC:

ios5 键盘高度变高覆盖ui与获取键盘高度的方法

背景:ios5之前,iphone上的键盘的高度是固定为216.0px高的,中文汉字的选择框是悬浮的,所以不少应用都将此高度来标注键盘的高度(包括米聊也是这么做的)。

  可是在ios5中,键盘布局变了,尤其是中文输入时,中文汉字选择框就固定在键盘上方,这样就使得原本与键盘紧密贴合的界面视图被中文汉字选择框给覆盖住了。一方面影响了界面的美观,另一方面,如果被覆盖的部分就是文本输入框的话,用户就无法看到输入的内容了。因此这个问题就必须得解决了。

解决方法:

  其实在一开始使用216.0px这个固定值来标注键盘的高度就是错误的。因为在ios3.2以后的系统中,苹果就提供了键盘使用的api以及demo程序——“KeyboardAccessory”。

  处理键盘事件的正确方法是这样的:(包括获取键盘的高度以及键盘弹出和消失动画的时间)

  1)在要使用键盘的视图控制器中,接收键盘事件的通知

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

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


        // 键盘高度变化通知,ios5.0新增的  

#ifdef __IPHONE_5_0

        float version = [[[UIDevice currentDevice] systemVersion] floatValue];

        if (version >= 5.0) {

            [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillChangeFrameNotification object:nil];

        }

#endif

2)然后添加键盘事件的处理代码:

    获取到当前keyboard的高度以及动画时间,然后对视图进行对应的操作即可。


#pragma mark -

#pragma mark Responding to keyboard events

- (void)keyboardWillShow:(NSNotification *)notification {

    

    /*

     Reduce the size of the text view so that it's not obscured by the keyboard.

     Animate the resize so that it's in sync with the appearance of the keyboard.

     */

    

    NSDictionary *userInfo = [notification userInfo];

    

    // Get the origin of the keyboard when it's displayed.

    NSValue* aValue = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];

    

    // Get the top of the keyboard as the y coordinate of its origin in self's view's coordinate system. The bottom of the text view's frame should align with the top of the keyboard's final position.

    CGRect keyboardRect = [aValue CGRectValue];

    

    // Get the duration of the animation.

    NSValue *animationDurationValue = [userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey];

    NSTimeInterval animationDuration;

    [animationDurationValue getValue:&animationDuration];

    

    // Animate the resize of the text view's frame in sync with the keyboard's appearance.

    [self moveInputBarWithKeyboardHeight:keyboardRect.size.height withDuration:animationDuration];

}



- (void)keyboardWillHide:(NSNotification *)notification {

    

    NSDictionary* userInfo = [notification userInfo];

    

    /*

     Restore the size of the text view (fill self's view).

     Animate the resize so that it's in sync with the disappearance of the keyboard.

     */

    NSValue *animationDurationValue = [userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey];

    NSTimeInterval animationDuration;

    [animationDurationValue getValue:&animationDuration];

    

    [self moveInputBarWithKeyboardHeight:0.0 withDuration:animationDuration];

}

  3)在视图控制器消除时,移除键盘事件的通知:

[[NSNotificationCenter defaultCenter] removeObserver:self];


转载于:https://my.oschina.net/u/2331935/blog/540973

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值