gpt4 book ai didi

ios - swift : scroll the view up when keyboard shows

转载 作者:IT王子 更新时间:2023-10-29 05:25:54 26 4
gpt4 key购买 nike

我有一个 scrollView,我想在显示键盘时向上滚动。

当键盘显示时,我因此错误而崩溃:

2014-09-29 14:48:50.738 swrd[1563:472888] -[swrd.EditPhotoViewController keyboardWasShown]:无法识别的选择器发送到实例 0x14ed36640

这是我的代码,有什么问题吗?:

   func registerForKeyboardNotifications ()-> Void   {

NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardWasShown", name: UIKeyboardDidShowNotification, object: nil)

NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardWillBeHidden", name: UIKeyboardWillHideNotification, object: nil)


}

func deregisterFromKeyboardNotifications () -> Void {
let center: NSNotificationCenter = NSNotificationCenter.defaultCenter()
center.removeObserver(self, name: UIKeyboardDidHideNotification, object: nil)
center.removeObserver(self, name: UIKeyboardWillHideNotification, object: nil)


}


func keyboardWasShown (notification: NSNotification) {

let info : NSDictionary = notification.userInfo!
let keyboardSize = info.objectForKey(UIKeyboardFrameBeginUserInfoKey)?.frame

let insets: UIEdgeInsets = UIEdgeInsetsMake(self.scrollView.contentInset.top, 0, keyboardSize!.height, 0)

self.scrollView.contentInset = insets
self.scrollView.scrollIndicatorInsets = insets

self.scrollView.contentOffset = CGPointMake(self.scrollView.contentOffset.x, self.scrollView.contentOffset.y + keyboardSize!.height)

}

func keyboardWillBeHidden (notification: NSNotification) {

let info : NSDictionary = notification.userInfo!
let keyboardSize = info.objectForKey(UIKeyboardFrameBeginUserInfoKey)?.frame

let insets: UIEdgeInsets = UIEdgeInsetsMake(self.scrollView.contentInset.top, 0, keyboardSize!.height, 0)

self.scrollView.contentInset = insets
self.scrollView.scrollIndicatorInsets = insets



}


override func viewWillAppear(animated: Bool) {
super.viewWillAppear(true)



self.registerForKeyboardNotifications()

}

override func viewWillDisappear(animated: Bool) {
super.viewWillDisappear(true)

self.deregisterFromKeyboardNotifications()

}

最佳答案

在您的代码中,keyboardWasShownkeyboardWasHidden 各有一个参数,NSNotification。您需要用冒号终止 addObserver 中的选择器,以便它通过。即,keyboardWasShownkeyboardWasShown: 是不同的选择器。

NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardWasShown:", name: UIKeyboardDidShowNotification, object: nil)

关于ios - swift : scroll the view up when keyboard shows,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26100545/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com