滚动查看和键盘Swift

我在这里是新的,即时从iOS的Swift开始。

我开始创build一个简单的应用程序,执行一些操作。 但是当键盘出现时,我遇到了一些问题,隐藏了我的一个textField。 我认为这是一个普遍的问题,我做了一些研究,但我找不到解决我的问题的任何东西。 我想使用Scroll而不是使textFieldanimation显示。

谢谢!!!!! (抱歉英文错误)

在ViewDidLoad中,注册通知:

NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(keyboardWillShow), name:UIKeyboardWillShowNotification, object: nil) NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(keyboardWillHide), name:UIKeyboardWillHideNotification, object: nil) 

添加下面的观察者方法,当键盘出现时自动滚动。

 func textFieldShouldReturn(textField: UITextField) -> Bool { textField.resignFirstResponder() return true } func keyboardWillShow(notification:NSNotification){ var userInfo = notification.userInfo! var keyboardFrame:CGRect = (userInfo[UIKeyboardFrameBeginUserInfoKey] as! NSValue).CGRectValue() keyboardFrame = self.view.convertRect(keyboardFrame, fromView: nil) var contentInset:UIEdgeInsets = self.scrollView.contentInset contentInset.bottom = keyboardFrame.size.height scrollView.contentInset = contentInset } func keyboardWillHide(notification:NSNotification){ let contentInset:UIEdgeInsets = UIEdgeInsetsZero scrollView.contentInset = contentInset } 

编辑:这个职位已经更新,以反映Swift 2.2 Objective Cselect器的最佳做法。

快速答案3:

 NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow), name:NSNotification.Name.UIKeyboardWillShow, object: nil) NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHide), name:NSNotification.Name.UIKeyboardWillHide, object: nil) 

接着:

 func keyboardWillShow(notification:NSNotification){ //give room at the bottom of the scroll view, so it doesn't cover up anything the user needs to tap var userInfo = notification.userInfo! var keyboardFrame:CGRect = (userInfo[UIKeyboardFrameBeginUserInfoKey] as! NSValue).cgRectValue keyboardFrame = self.view.convert(keyboardFrame, from: nil) var contentInset:UIEdgeInsets = self.theScrollView.contentInset contentInset.bottom = keyboardFrame.size.height theScrollView.contentInset = contentInset } func keyboardWillHide(notification:NSNotification){ let contentInset:UIEdgeInsets = UIEdgeInsets.zero theScrollView.contentInset = contentInset } 

阅读你发给我的链接,我find了一种方法,使其工作,谢谢!

 func textFieldDidBeginEditing(textField: UITextField) { if (textField == //your_field) { scrollView.setContentOffset(CGPointMake(0, field_extra.center.y-280), animated: true) callAnimation() viewDidLayoutSubviews() } } func textFieldDidEndEditing(textField: UITextField) { if (textField == //your_field){ scrollView .setContentOffset(CGPointMake(0, 0), animated: true) viewDidLayoutSubviews() } } 

contentInset不适用于我,因为我想让scrollview一直向上移动到键盘上方。 所以我使用contentOffset

 func keyboardWillShow(notification:NSNotification) { guard let keyboardFrameValue = notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue else { return } let keyboardFrame = view.convert(keyboardFrameValue.cgRectValue, from: nil) scrollView.contentOffset = CGPoint(x:0, y:keyboardFrame.size.height) } func keyboardWillHide(notification:NSNotification) { scrollView.contentOffset = .zero } 

您可以通过滚动偏移将您的滚动视图设置为以键盘外观的UITextField为中心(即,使您的文本字段成为第一个响应者)。 这里有几个很好的资源,让你开始(这个网站上有一堆):

如何以编程方式移动UIScrollView以在键盘上方的控件中进行对焦?

如何在UITextField成为第一响应者时进行UIScrollView自动滚动

另外,如果你只是在单元格中使用UITableView和你的内容,当文本字段成为第一响应者时,UITableViewController会自动滚动到文本字段单元(尽pipe我不确定这是你想要做的)。

Swift 3的答案基于Daniel Jones提出的答案,但更安全(感谢后卫),更简洁,并带有一致的滚动指标插入:

 @objc private func keyboardWillBeShown(notification: NSNotification) { guard let keyboardFrameValue = notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue else { return } let keyboardFrame = view.convert(keyboardFrameValue.cgRectValue, from: nil) scrollView.contentInset.bottom = keyboardFrame.size.height scrollView.scrollIndicatorInsets = scrollView.contentInset } @objc private func keyboardWillBeHidden() { scrollView.contentInset = .zero scrollView.scrollIndicatorInsets = scrollView.contentInset } 

Swift 3是一个完整的解决scheme,利用守卫和简洁的代码。 在keyboardWillHide加上正确的代码只能将bottom重置为0。

 @IBOutlet weak var scrollView: UIScrollView! override func viewWillAppear(_ animated: Bool) { super.viewWillAppear(animated) registerNotifications() } override func viewWillDisappear(_ animated: Bool) { super.viewWillDisappear(animated) unregisterNotifications() } func registerNotifications() { NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow), name: .UIKeyboardWillShow, object: nil) NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHide), name: .UIKeyboardWillHide, object: nil) } func unregisterNotifications() { NotificationCenter.default.removeObserver(self, name: .UIKeyboardWillShow, object: nil) NotificationCenter.default.removeObserver(self, name: .UIKeyboardWillHide, object: nil) } func keyboardWillShow(notification: NSNotification){ guard let keyboardFrame = notification.userInfo![UIKeyboardFrameBeginUserInfoKey] as? NSValue else { return } scrollView.contentInset.bottom = view.convert(keyboardFrame.cgRectValue, from: nil).size.height + 20 } func keyboardWillHide(notification: NSNotification){ scrollView.contentInset.bottom = 0 } 
 override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib. NotificationCenter.default.addObserver(self, selector: #selector(ViewController.keyboardWillShow), name: NSNotification.Name.UIKeyboardWillShow, object: nil) NotificationCenter.default.addObserver(self, selector: #selector(ViewController.keyboardWillHide), name: NSNotification.Name.UIKeyboardWillHide, object: nil) } func keyboardWillShow(_ notification:Notification) { if let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue { tableView.contentInset = UIEdgeInsetsMake(0, 0, keyboardSize.height, 0) } } func keyboardWillHide(_ notification:Notification) { if let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue { tableView.contentInset = UIEdgeInsetsMake(0, 0, 0, 0) } }