Swift:来自UIViewController扩展的registerForKeyboardNotifications

Man*_*tof 3 keyboard ios swift

我正在尝试清理我的代码并创建和扩展UIViewController以注册和取消注册我的键盘.在我的实用程序文件中,我有以下内容:

extension UIViewController {
     //Register the keyboard for notifications in  viewDidLoad
     func registerForKeyboardNotifications() {

         //Adding notifies on keyboard appearing
         NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(CreateEventVC.keyboardWasShown(_:)), name: UIKeyboardWillShowNotification, object: nil) 
         NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(CreateEventVC.keyboardWillBeHidden(_:)), name: UIKeyboardWillHideNotification, object: nil)

     }

     //Deregister the keyboard for notification in viewWillDisapper
     func deregisterFromKeyboardNotifications() {

         //Removing notifies on keyboard appearing
         NSNotificationCenter.defaultCenter().removeObserver(self, name: UIKeyboardWillShowNotification, object: nil)
         NSNotificationCenter.defaultCenter().removeObserver(self, name: UIKeyboardWillHideNotification, object: nil)
     }
}
Run Code Online (Sandbox Code Playgroud)

我需要的是"CreateEventVC"在selector为当前Class.

然后我用我的ViewController调用它,如下所示:

override func viewDidLoad() {
         super.viewDidLoad()

         //Register Keyboard
         registerForKeyboardNotifications()
}

override func viewWillDisappear(animated: Bool) {

    deregisterFromKeyboardNotifications()

}

func keyboardWasShown(notification: NSNotification) {

    if let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.CGRectValue() {

        self.view.frame.origin.y -= keyboardSize.height

    }

}

func keyboardWillBeHidden(notification: NSNotification) {

    if let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.CGRectValue() {

        self.view.frame.origin.y += keyboardSize.height

    }

}
Run Code Online (Sandbox Code Playgroud)

我觉得这不是最好的做法.任何帮助或建议都会很棒!

noi*_*gle 11

我正在使用UIViewControllerUIScrollView扩展.如果你正在转移UIView你也可以使用这个扩展,但使用UIScrollView更方便,因为它不会搞乱你的视图边界,如果你需要添加更多的文本字段,这种方法更通用.

extension UIViewController {
    func registerForKeyboardDidShowNotification(usingBlock block: ((NSNotification, CGSize) -> Void)? = nil) {
        NSNotificationCenter.defaultCenter().addObserverForName(UIKeyboardDidShowNotification, object: nil, queue: nil, usingBlock: { (notification) -> Void in
            let userInfo = notification.userInfo!
            guard let keyboardSize = userInfo[UIKeyboardFrameBeginUserInfoKey]?.CGRectValue.size else { fatalError("Can't grab the keyboard frame") }

            block?(notification, keyboardSize)
        })
    }

    func registerForKeyboardWillHideNotification(usingBlock block: (NSNotification -> Void)? = nil) {
        NSNotificationCenter.defaultCenter().addObserverForName(UIKeyboardWillHideNotification, object: nil, queue: nil, usingBlock: { (notification) -> Void in
            block?(notification)
        })
    }
}
Run Code Online (Sandbox Code Playgroud)

所以与此代码,你UIViewControllerviewDidLoad()方法,你可以为键盘通知注册和块添加任何附加功能.像这样:

override func viewDidLoad() {
    super.viewDidLoad()

    registerForKeyboardDidShowNotification { notif, size in
        self.view.frame.origin.y -= size.height
    }

    registerForKeyboardWillHideNotification { notif in
        self.view.frame.origin.y = 0
    }
}
Run Code Online (Sandbox Code Playgroud)

编辑:( For UIScrollView)

1)你能为键盘显示动画吗?

实际上键盘外观总是动画的,所以我认为你说的是​​scrollview调整.是的你可以.下面我发布了更新UIViewControllerUIScrollView扩展,正在处理它.

2)我是否需要取消注册键盘?

那么,避免将来发生崩溃是一种很好的做法.为此,您可能更愿意viewWillAppearviewWillDisappear方法中添加通知并将其删除.

    NSNotificationCenter.defaultCenter().removeObserver(scrollView, name: UIKeyboardDidShowNotification, object: nil)
    NSNotificationCenter.defaultCenter().removeObserver(scrollView, name: UIKeyboardWillHideNotification, object: nil)
Run Code Online (Sandbox Code Playgroud)

3)对于UIScrollView方法,我将所有对象放在屏幕的滚动视图中,然后只需在每个UITextField成为第一个响应者时以编程方式滚动视图?

您不以编程方式滚动任何内容.您需要做的是更改插图,UIScrollView它将自动使第一个响应者在屏幕上可见.

这是我在项目中使用的代码:

extension UIViewController {
    func registerForKeyboardDidShowNotification(scrollView: UIScrollView, usingBlock block: (CGSize? -> Void)? = nil) {
        NSNotificationCenter.defaultCenter().addObserverForName(UIKeyboardDidShowNotification, object: nil, queue: nil, usingBlock: { (notification) -> Void in
            let userInfo = notification.userInfo!
            let keyboardSize = userInfo[UIKeyboardFrameEndUserInfoKey]?.CGRectValue.size
            let contentInsets = UIEdgeInsetsMake(scrollView.contentInset.top, scrollView.contentInset.left, keyboardSize!.height, scrollView.contentInset.right)

            scrollView.setContentInsetAndScrollIndicatorInsets(contentInsets)
            block?(keyboardSize)
        })
    }

    func registerForKeyboardWillHideNotification(scrollView: UIScrollView, usingBlock block: (Void -> Void)? = nil) {
        NSNotificationCenter.defaultCenter().addObserverForName(UIKeyboardWillHideNotification, object: nil, queue: nil, usingBlock: { (notification) -> Void in
            let contentInsets = UIEdgeInsetsMake(scrollView.contentInset.top, scrollView.contentInset.left, 0, scrollView.contentInset.right)
            scrollView.setContentInsetAndScrollIndicatorInsets(contentInsets)
            block?()
        })
    }
}



extension UIScrollView {
    func setContentInsetAndScrollIndicatorInsets(edgeInsets: UIEdgeInsets) {
        self.contentInset = edgeInsets
        self.scrollIndicatorInsets = edgeInsets
    }
}
Run Code Online (Sandbox Code Playgroud)

在你的viewWillAppear方法中,只需添加如下:

registerForKeyboardWillShowNotification(scrollView)
registerForKeyboardWillHideNotification(scrollView)
Run Code Online (Sandbox Code Playgroud)

每当键盘出现时它都会缩小scrollview的contentInset,并在键盘消失时展开它.