gpt4 book ai didi

ios - Swift:ScrollRectToVisible 不工作

转载 作者:IT王子 更新时间:2023-10-29 05:48:35 27 4
gpt4 key购买 nike

在尝试了此处发布的大部分解决方案后,我仍然无法移动文本字段以在 ScrollView 中显示在键盘上方。

这些是我从 Stackoverflow 解决方案中获取的链接: 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
}

如您所见,我已尝试使用 frame 的 scrollRectToVisible 和使用 Point 的 setContentOffset。两者都不起作用。而代码选择了 emailTextField 作为隐藏的文本字段。

最佳答案

我也遇到了和你一样的问题,我不知道你是否成功并找到了解决方案,但我最终使用 setContentOffset 函数而不是 scrollRectToVisible 并且它起作用了。

swift 3.x 示例:

if (!aRect.contains(activeTextView!.frame.origin)) {
self.scrollView.setContentOffset(CGPoint(x:0, y:self.activeTextView!.frame.origin.y), animated: true)
}

关于ios - Swift:ScrollRectToVisible 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38099322/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com