在尝试了这里发布的大多数解决方案之后,我仍然无法移动文本字段以在滚动视图中显示在键盘上方。
这些是我从Stackoverflow解决方案中获得的链接: Link 1 Link 2 Link 3
我正在注册屏幕上工作,当屏幕显示时,键盘后面有一个字段。
这是我的代码:
class SignUpViewController: UIViewController, UITextFieldDelegate, UIScrollViewDelegate, UIPopoverPresentationControllerDelegate {
@IBOutlet var firstNameTextField: UITextField!
@IBOutlet var lastNameTextField: UITextField!
@IBOutlet var phoneNumberTextField: UITextField!
@IBOutlet var emailTextField: UITextField!
@IBOutlet var submitButton: UIButton!
@IBOutlet var professionButton: UIButton!
var scrollView: UIScrollView?
var activeTextField:UITextField? = UITextField()
override func viewDidLoad() {
super.viewDidLoad()
let notificationCenter = NSNotificationCenter.defaultCenter()
notificationCenter.addObserver(self, selector: #selector(SignUpViewController.keyboardWasShown(_:)), name: UIKeyboardWillShowNotification, object: nil)
notificationCenter.addObserver(self, selector: #selector(SignUpViewController.keyboardWillBeHidden(_:)), name: UIKeyboardWillHideNotification, object: nil)
scrollView = UIScrollView(frame: CGRect(x: 0, y: 0, width: self.view.frame.size.width, height: self.view.frame.size.height))
scrollView!.contentSize = CGSizeMake(self.view.frame.width, self.view.frame.height)
defaultSettings()
}
func defaultSettings() {
self.firstNameTextField.delegate = self
self.lastNameTextField.delegate = self
self.emailTextField.delegate = self
self.phoneNumberTextField.delegate = self
}
func deregisterFromKeyboardNotifications()
{
//Removing notifies on keyboard appearing
NSNotificationCenter.defaultCenter().removeObserver(self, name: UIKeyboardWillShowNotification, object: nil)
NSNotificationCenter.defaultCenter().removeObserver(self, name: UIKeyboardWillHideNotification, object: nil)
}
func keyboardWasShown(notification: NSNotification)
{
//Need to calculate keyboard exact size due to Apple suggestions
//self.scrollView!.scrollEnabled = true
var info : NSDictionary = notification.userInfo!
var keyboardSize = (info[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.CGRectValue().size
var contentInsets : UIEdgeInsets = UIEdgeInsetsMake(0.0, 0.0, keyboardSize!.height, 0.0)
self.scrollView!.contentInset = contentInsets
self.scrollView!.scrollIndicatorInsets = contentInsets
var aRect : CGRect = self.view.frame
aRect.size.height -= keyboardSize!.height
if (!CGRectContainsPoint(aRect, activeTextField!.frame.origin))
{
// print(activeTextField?.frame)
// var scrollPoint = CGPointMake(0.0, activeTextField!.frame.origin.y - (keyboardSize!.height-15))
self.scrollView!.scrollRectToVisible((activeTextField?.frame)!, animated: true)
//self.scrollView?.setContentOffset(scrollPoint, animated: true)
}
}
func keyboardWillBeHidden(notification: NSNotification)
{
//Once keyboard disappears, restore original positions
//var info : NSDictionary = notification.userInfo!
//var keyboardSize = (info[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.CGRectValue().size
var contentInsets : UIEdgeInsets = UIEdgeInsetsZero
self.scrollView!.contentInset = contentInsets
self.scrollView!.scrollIndicatorInsets = contentInsets
// self.view.endEditing(true)
// self.scrollView!.scrollEnabled = false
}
func textFieldDidBeginEditing(textField: UITextField)
{
activeTextField = textField
}
func textFieldDidEndEditing(textField: UITextField)
{
activeTextField = nil
}
正如您所看到的,我已尝试使用frame的scrollRectToVisible和使用Point的setContentOffset。两者都没有奏效。而代码选择emailTextField作为隐藏文本字段。
答案 0 :(得分:13)
我也遇到了和你一样的问题,我不知道你是否成功并找到了解决方案,但我最后使用了setContentOffset函数而不是scrollRectToVisible而且它有效。
Swift 3.x示例:
function HandleClick(e) {
e.preventDefault();
...
}
答案 1 :(得分:0)
您还应该将scrollView委托设置为:
self.scrollView.delegate = self as! UIScrollViewDelegate
ScrollView委托方法与此配合使用。
答案 2 :(得分:0)
SWIFT 4 尝试
scrollView.scrollRectToVisible(myElementView.frame, animated: true)
其中myElementView
可以是任何元素