带NSNotificationCenter的swift键盘



我正在尝试将obj-c代码转换为swift,以移动位于键盘下的内容。我在keyboardWillShown()方法中遇到了一个名为"CGPoint不能转换为CGRect"的问题。我不知道该怎么处理。下面是我的代码部分-

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")
}

更新!

键盘和文本视图,这可能接近我们想要的,试试看。

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

在您的视图中,DidLoad 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测试版4起到了作用。希望这能帮助到别人。

最新更新