键盘与NSNotificationCenter在swift中

时间:2015-02-16 11:57:30

标签: swift keyboard nsnotificationcenter

我试图将obj-c代码转换为swift以移动位于键盘下方的内容。我有一个名为&#34的问题; CGPoint不能转换为CGRect"在keyboardWillShown()方法中。我不知道如何处理这个问题。下面是我的代码部分 -

func registerForKeyboardNotifications (){
    var center1 = NSNotificationCenter.defaultCenter()
    center1.addObserver(self, selector: Selector("keyboardWillShown"), name: UIKeyboardWillShowNotification, object: nil)

    var center2 = NSNotificationCenter.defaultCenter()
    center2.addObserver(self, selector: Selector("keyboardWillBeHidden"), name: UIKeyboardWillHideNotification, object: nil)
}

func registerForKeyboardNotifications (){
    var center1 = NSNotificationCenter.defaultCenter()
    center1.addObserver(self, selector: Selector("keyboardWillShown"), name: UIKeyboardWillShowNotification, object: nil)

    var center2 = NSNotificationCenter.defaultCenter()
    center2.addObserver(self, selector: Selector("keyboardWillBeHidden"), name: UIKeyboardWillHideNotification, object: nil)
}

func keyboardWillShown(notification: NSNotification) {
    if let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.CGRectValue() {
        let contentInsets = UIEdgeInsets(top: 0, left: 0, bottom: keyboardSize.height, right: 0)
        self.scrollView.contentInset  = contentInsets
        self.scrollView.scrollIndicatorInsets = contentInsets

        // If active text field is hidden by keyboard, scroll it so it's visible
        var aRect : CGRect = self.view.frame
        aRect.size.height -= keyboardSize.height
        if !CGRectContainsPoint(aRect, self.textView.frame.origin) {
            //This below line shows error
            self.scrollView.scrollRectToVisible(self.textView.frame.origin, animated: true)
        }
    }
}

func keyboardWillBeHidden() {
    println("Keyboard hidden")
}

1 个答案:

答案 0 :(得分:0)

已更新!

键盘和文字视图,这可能接近我们正在寻找的东西,尝试一下。

在此代码中,我不使用scrollView而是移动textView.frame,而是使用contentInset来跟踪光标。

在你的viewDidLoad registerForKeyboardNotification中,将你的委托设置为self:

override func viewDidLoad() {
    registerForKeyboardNotification()
    textView.delegate = self
}

然后你喜欢:

func registerForKeyboardNotification() {
    NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardWasShown:"), name: UIKeyboardDidShowNotification, object: nil)
    NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardWillBeHidden:"), name: UIKeyboardWillHideNotification, object: nil)
}

func keyboardWasShown(aNotification: NSNotification) {
    let info = aNotification.userInfo!
    let kbSize = (info[UIKeyboardFrameBeginUserInfoKey])!.CGRectValue.size
    let contentInsets = UIEdgeInsetsMake(0.0, 0.0, kbSize.height, 0.0)
    textView.contentInset = contentInsets
    textView.scrollIndicatorInsets = contentInsets
}

func keyboardWillBeHidden(aNotification: NSNotification) {
    let contentInsets = UIEdgeInsetsZero
    textView.contentInset = contentInsets
    textView.scrollIndicatorInsets = contentInsets
    textView.resignFirstResponder()
}

Xcode 7 beta 4对我有用。希望能有所帮助。