Swift 3 NSNotificationCenter KeyboardWill显示/隐藏 [英] Swift 3 NSNotificationCenter Keyboardwillshow/hide

查看:691
本文介绍了Swift 3 NSNotificationCenter KeyboardWill显示/隐藏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一段代码在Swift 2中工作,我尝试使用xCode将代码更新到最新版本,我修复了除了两个问题之外的所有内容

I have a piece of code that worked in Swift 2 and i tried using xCode to update the code to the newest version and i fixed everything except two issues

I有这个代码

let loginvc : LoginVC = self.storyboard?.instantiateViewController(withIdentifier: "LoginVC") as! LoginVC
NotificationCenter.defaultCenter().addObserver(self, selector: #selector(LoginViewController.keyboardWillShow(_:)), name: UIKeyboardWillShowNotification, object: nil)
NotificationCenter.defaultCenter().addObserver(self, selector: #selector(LoginViewController.keyboardWillHide(_:)), name: UIKeyboardWillHideNotification, object: nil)

与此一起配对

func keyboardWillShow(notification: NSNotification) {

    constraint.constant = -100
    UIView.animate(withDuration: 0.3) {
        self.view.layoutIfNeeded()
    }
}

func keyboardWillHide(notification: NSNotification) {

    constraint.constant = 25
    UIView.animate(withDuration: 0.3) {
        self.view.layoutIfNeeded()
    }
}

在第一部分我现在得到一个错误,说类型'LoginViewController'没有成员'keyboardwillshow /隐藏'

On the first part I now get an error saying "Type 'LoginViewController' has no member 'keyboardwillshow/hide'

我不明白为什么它没有看到下面的方法

I don't understand why it is not seeing the method underneath

有没有人知道这个问题的解决方案?

Does anybody know a solution to this issue?

推荐答案

查看更新的 Swift编程语言书。第1027和1028页是您正在寻找的。它应该是这样的:

Check out the updated Swift Programming Language book. Pages 1027 and 1028 are what you're looking for. It should be something like this:

func keyboardWillHide(_ notification: NSNotification) {…

请注意上面的附加下划线。另外:

Notice the additional underscore above. Also:

#selector(LoginViewController.keyboardWillHide(_:))

您还可能需要在课堂上添加 @objc(keyboardWillHideWithNotification :)

You also might need to add @objc(keyboardWillHideWithNotification:) to your class.

这篇关于Swift 3 NSNotificationCenter KeyboardWill显示/隐藏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆