尝试了最贴在这里的解决方案后,我仍然有麻烦移动文本框来显示键盘上方的滚动视图。
这是我的链接从解决方案#1如下:Link 1 Link 2 Link 3
我工作具有键盘落后1场,当它显示了一个注册屏幕上。
这里是我的代码:
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
}
正如你所看到的,我已经试过scrollRectToVisible与框架和setContentOffset与点。两人都没有工作。尽管代码在emailTextField挑选权作为隐藏文本框。
我也用同样的问题做斗争,你这样做,我不知道,如果你是成功的,找到解决办法,但我终于用setContentOffset函数,而不是scrollRectToVisible和它的工作。
斯威夫特3.X例如:
if (!aRect.contains(activeTextView!.frame.origin)) {
self.scrollView.setContentOffset(CGPoint(x:0, y:self.activeTextView!.frame.origin.y), animated: true)
}
你也应该设置了滚动委托为:
self.scrollView.delegate = self as! UIScrollViewDelegate
滚动型的委托方法有这方面的工作。
SWIFT 4试试这个
scrollView.scrollRectToVisible(myElementView.frame, animated: true)
其中myElementView
可以是任何元件