tnblog
首页

ios 键盘弹出时获取键盘高度

68人阅读 2019/1/7 20:49 评论:0 收藏
分类: iOS

Objective-C实现:

    //增加监听,当键盘出现或改变时收出消息

    [[NSNotificationCenter defaultCenter] addObserver:self

                                             selector:@selector(keyboardWillShow:)

                                                 name:UIKeyboardWillShowNotification

                                               object:nil];

    

    //当键盘出现或改变时调用

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

        //获取键盘的高度    

        NSDictionary *userInfo = [aNotification userInfo];

        NSValue *aValue = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];

        CGRect keyboardRect = [aValue CGRectValue];

        int height = keyboardRect.size.height;

     }



swift实现:

    NotificationCenter.default.addObserver(self, selector: #selector(keybordShow(notification:)), name: NSNotification.Name.UIKeyboardWillShow, object: nil)

    //键盘弹起

    func keybordShow(notification:Notification)  {

        let userinfo: NSDictionary = notification.userInfo! as NSDictionary

        let nsValue = userinfo.object(forKey: UIKeyboardFrameEndUserInfoKey) as! NSValue

        let keyboardRec = nsValue.cgRectValue

        let height = keyboardRec.size.height

        print("keybordShow:\(height)")

    }




评价

{{item.title}}

{{item.content}}
没有个性,不需要签名
博主搜索
文章类别
最新文章
最新评价
{{item.ArticleTitle}}
{{item.BlogName}} : {{item.Content}}