Swift:当键盘显示时向上滚动视图
Posted
技术标签:
【中文标题】Swift:当键盘显示时向上滚动视图【英文标题】:Swift : scroll the view up when keyboard shows 【发布时间】:2014-11-23 21:13:07 【问题描述】:我有一个 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()
【问题讨论】:
【参考方案1】:在您的代码中,keyboardWasShown
和 keyboardWasHidden
各有一个参数,NSNotification
。您需要用冒号终止 addObserver
中的选择器,以便它通过。即,keyboardWasShown
和 keyboardWasShown:
是不同的选择器。
NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardWasShown:", name: UIKeyboardDidShowNotification, object: nil)
【讨论】:
【参考方案2】:请参阅下面的独立解决方案:
private func startObservingKeyboardEvents()
NSNotificationCenter.defaultCenter().addObserver(self,
selector:Selector("keyboardWillShow:"),
name:UIKeyboardWillShowNotification,
object:nil)
NSNotificationCenter.defaultCenter().addObserver(self,
selector:Selector("keyboardWillHide:"),
name:UIKeyboardWillHideNotification,
object:nil)
private func stopObservingKeyboardEvents()
NSNotificationCenter.defaultCenter().removeObserver(self, name: UIKeyboardWillShowNotification, object: nil)
NSNotificationCenter.defaultCenter().removeObserver(self, name: UIKeyboardWillHideNotification, object: nil)
func keyboardWillShow(notification: NSNotification)
if let userInfo = notification.userInfo
if let keyboardSize: CGSize = userInfo[UIKeyboardFrameEndUserInfoKey]?.CGRectValue().size
let contentInset = UIEdgeInsetsMake(0.0, 0.0, keyboardSize.height, 0.0);
func keyboardWillHide(notification: NSNotification)
let contentInset = UIEdgeInsetsZero;
使用contentInset
变量调整内容插入。
【讨论】:
感谢您提供所有代码,很棒的东西,谢谢!【参考方案3】:就我而言,需要对上面的代码进行一些更改:
1 - 首先,您需要在视图中放置一个 Scrollview,并设置如下约束:
滚动视图比视图更重要。
2 - 放置您需要的 TextField 和其他组件。
3 - 使用 @IBOutlet 将 ScrollView 链接到 ViewController
4 - 将委托添加到 ScrollView:
class ViewController: UIViewController, UITextFieldDelegate, UIScrollViewDelegate
...
5 - 添加观察者:
override func viewWillAppear(animated: Bool)
self.startKeyboardObserver()
override func viewWillDisappear(animated: Bool)
self.stopKeyboardObserver()
private func startKeyboardObserver()
NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardWillShow:", name: UIKeyboardWillShowNotification, object: nil) //WillShow and not Did ;) The View will run animated and smooth
NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardWillHide:", name: UIKeyboardWillHideNotification, object: nil)
private func stopKeyboardObserver()
NSNotificationCenter.defaultCenter().removeObserver(self, name: UIKeyboardWillShowNotification, object: nil)
NSNotificationCenter.defaultCenter().removeObserver(self, name: UIKeyboardWillHideNotification, object: nil)
6 - 添加滚动代码,计算 KeyboardSize。
func keyboardWillShow(notification: NSNotification)
if let userInfo = notification.userInfo
if let keyboardSize: CGSize = userInfo[UIKeyboardFrameEndUserInfoKey]?.CGRectValue().size
let contentInset = UIEdgeInsetsMake(0.0, 0.0, keyboardSize.height, 0.0);
self.scrollView.contentInset = contentInset
self.scrollView.scrollIndicatorInsets = contentInset
self.scrollView.contentOffset = CGPointMake(self.scrollView.contentOffset.x, 0 + keyboardSize.height) //set zero instead self.scrollView.contentOffset.y
func keyboardWillHide(notification: NSNotification)
if let userInfo = notification.userInfo
if let keyboardSize: CGSize = userInfo[UIKeyboardFrameEndUserInfoKey]?.CGRectValue().size
let contentInset = UIEdgeInsetsZero;
self.scrollView.contentInset = contentInset
self.scrollView.scrollIndicatorInsets = contentInset
self.scrollView.contentOffset = CGPointMake(self.scrollView.contentOffset.x, self.scrollView.contentOffset.y)
【讨论】:
键盘出现时有闪烁效果。第一个视图向上移动,然后它会来到点击字段以上是关于Swift:当键盘显示时向上滚动视图的主要内容,如果未能解决你的问题,请参考以下文章
NavController 中的滚动视图在显示键盘后不会重置(Swift)