下面是我尝试使用的代码,只有在键盘隐藏了聚焦的textField或UIButton时才能滚动视图:
import UIKit
class ViewController: UIViewController, UITextFieldDelegate {
@IBOutlet weak var buttonToBottomConstraint: NSLayoutConstraint!
@IBOutlet weak var textFieldUsername: UITextField!
@IBOutlet weak var textFieldPassword: UITextField!
@IBOutlet weak var scrollView: UIScrollView!
override func viewDidLoad() {
super.viewDidLoad()
NSNotificationCenter.defaultCenter().addObserver(self, selector:"keyboardWillBeHidden:", name: UIKeyboardWillHideNotification, object: nil)
NSNotificationCenter.defaultCenter().addObserver(self, selector:"keyboardWillBeShown:", name: UIKeyboardWillShowNotification, object: nil)
}
func textFieldDidBeginEditing(textField: UITextField) {
}
func textFieldShouldReturn(textField: UITextField) -> Bool {textField.resignFirstResponder()
return true
}
func keyboardWillBeHidden(notification: NSNotification){
self.scrollView.setContentOffset(CGPointMake(0, 0), animated: true)
}
func keyboardWillBeShown(notification: NSNotification){
let userInfo = notification.userInfo!
if let kbsize = userInfo[UIKeyboardFrameBeginUserInfoKey]?.CGRectValue().size{
self.scrollView .setContentOffset(CGPointMake(0, kbsize.or), animated: true)
}
}
}
问题:即使键盘没有隐藏任何内容,View也会滚动。
启动时查看:
点击TextField时查看:
问题:如何检查键盘是否隐藏了聚焦区域,如果是,则将其推到上方?
修改
应用程序在纵向模式下工作正常,但不适用于横向模式。同样在横向模式下,当用户名textField键盘启动并点击密码textField时,它仍然隐藏在键盘后面。我在Github上更新了项目并修复了链接。感谢
横向拍摄的屏幕截图:
答案 0 :(得分:1)
var currentTextField:UITextField?
@IBOutlet weak var textFieldUsername: UITextField!
@IBOutlet weak var textFieldPassword: UITextField!
@IBOutlet weak var scrollView: UIScrollView!
override func viewDidLoad()
{
super.viewDidLoad()
NSNotificationCenter.defaultCenter().addObserver(self, selector:"keyboardWillBeHidden:", name: UIKeyboardWillHideNotification, object: nil)
NSNotificationCenter.defaultCenter().addObserver(self, selector:"keyboardWillBeShown:", name: UIKeyboardWillShowNotification, object: nil)
}
func textFieldDidBeginEditing(textField: UITextField)
{
currentTextField=textField;
}
func textFieldShouldReturn(textField: UITextField) -> Bool {
textField.resignFirstResponder()
return true
}
func keyboardWillBeHidden(notification: NSNotification){
currentTextField=nil;
self.scrollView.setContentOffset(CGPointMake(0, 0), animated: true)
}
func keyboardWillBeShown(notification: NSNotification){
let userInfo = notification.userInfo!
if let kbsize = userInfo[UIKeyboardFrameBeginUserInfoKey]?.CGRectValue().size
{
let contentInsets = UIEdgeInsets(top: 0, left: 0, bottom: kbsize.height, right: 0)
let screenSize: CGRect = UIScreen.mainScreen().bounds
let screenHeight = screenSize.height
let height:CGFloat=screenHeight-(kbsize.height)-(currentTextField!.frame.size.width)
if(currentTextField!.frame.origin.y>=height )
{
var scrollPoint: CGPoint = CGPointMake(0.0, currentTextField!.frame.origin.y - (kbsize.height));
self.scrollView .setContentOffset(scrollPoint, animated: true)
}
}
}
func textFieldDidEndEditing(textField: UITextField) {
currentTextField=nil;
}
答案 1 :(得分:0)
我没有看到您设置scrollView
的{{1}}。因此,contentSize
使用键盘滚动,因为它使用屏幕的大小。