xcode Swift:ScrollRectToVisible 不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38099322/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Swift: ScrollRectToVisible not working
提问by Viginesh
After trying out most of the solutions posted here, I'm still having trouble moving the textfield to show up above keyboard in a scrollview.
在尝试了此处发布的大多数解决方案后,我仍然无法移动文本字段以在滚动视图中显示在键盘上方。
These are links I followed from Stackoverflow solutions: Link 1Link 2Link 3
这些是我从 Stackoverflow 解决方案中遵循的链接: 链接 1链接 2链接 3
I'm working on a signup screen that has 1 field behind the keyboard when it shows up.
我正在处理一个注册屏幕,当它出现时,键盘后面有 1 个字段。
Here's my code:
这是我的代码:
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
}
As you can see, I've tried scrollRectToVisible with frame and setContentOffset with Point. Both didn't work. Whereas the code picks on the emailTextField right as the hidden textfield.
如您所见,我已经尝试过使用 frame 的 scrollRectToVisible 和使用 Point 的 setContentOffset。两者都不起作用。而代码选择 emailTextField 作为隐藏的文本字段。
回答by ivo
I was also struggling with the same issue as you did, I don't know if you were successful and found solution, but I finally used setContentOffset function instead of scrollRectToVisible and it worked.
我也和你一样遇到了同样的问题,我不知道你是否成功并找到了解决方案,但我最终使用了 setContentOffset 函数而不是 scrollRectToVisible 并且它起作用了。
Swift 3.x example:
Swift 3.x 示例:
if (!aRect.contains(activeTextView!.frame.origin)) {
self.scrollView.setContentOffset(CGPoint(x:0, y:self.activeTextView!.frame.origin.y), animated: true)
}
回答by álvaro Agüero
SWIFT 4Try this
SWIFT 4试试这个
scrollView.scrollRectToVisible(myElementView.frame, animated: true)
where myElementView
can be any element
wheremyElementView
可以是任何元素
回答by user3107831
You should also set the scrollView delegate as:
您还应该将 scrollView 委托设置为:
self.scrollView.delegate = self as! UIScrollViewDelegate
ScrollView delegate methods work with this.
ScrollView 委托方法适用于此。