gpt4 book ai didi

ios - 当键盘出现时,如何将最后一个文本字段的 View 向上移动?

转载 作者:行者123 更新时间:2023-11-30 12:49:59 25 4
gpt4 key购买 nike

我的内部有一个 View ,其中有 6 个文本字段,但我想在单击最后一个文本字段时向上移动 View ,这样我可以在最后一个文本字段中输入值。

这是我的代码,但它适用于所有文本字段:-

 func animateViewMoving (up:Bool, moveValue :CGFloat){

let movementDuration:TimeInterval = 0.3
let movement:CGFloat = ( up ? -moveValue : moveValue)

UIView.beginAnimations("animateView", context: nil)
UIView.setAnimationBeginsFromCurrentState(true)
UIView.setAnimationDuration(movementDuration)

self.view.frame = self.view.frame.offsetBy(dx: 0, dy: movement)
UIView.commitAnimations()
}

因此,对于特定的文本字段,如何向上移动 View (键盘大小)

谢谢

最佳答案

@IBOutlet var scrollView: UIScrollView!
@IBOutlet var contentView: UIView!
var textFieldActive:UITextField!

override func viewDidLoad() {
super.viewDidLoad()

//Keyboard notification
NotificationCenter.default.addObserver(self,selector: #selector(keyboardWasShown),name:NSNotification.Name.UIKeyboardDidShow,
object: nil)
NotificationCenter.default.addObserver(self,selector: #selector(keyboardWillHide),name:NSNotification.Name.UIKeyboardWillHide,
object: nil)
// Do any additional setup after loading the view.
}



func textFieldDidBeginEditing(_ textField: UITextField){

textFieldActive = textField
}



// MARK: - Keyboard notification Method
func keyboardWasShown(notification: NSNotification)
{
let userInfo = notification.userInfo
let keyboardSize = (userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue
let contentInset = UIEdgeInsetsMake(0.0, 0.0, (keyboardSize?.height)!+100, 0.0)
self.scrollViewGroupMakeTakers.contentInset = contentInset
self.scrollViewGroupMakeTakers.scrollIndicatorInsets = contentInset

// Step 3: Scroll the target text field into view.
var aRect: CGRect = self.contentViewMakeTakers.frame;
aRect.size.height -= (keyboardSize?.height)!+100
if (!aRect.contains(textFieldActive!.frame.origin))
{

let scrollPoint: CGPoint = CGPoint(x: 0.0, y: textFieldActive!.frame.origin.y - (keyboardSize!.height-150));
self.scrollViewGroupMakeTakers.setContentOffset(scrollPoint, animated: true)
}
}
func keyboardWillHide(notification: NSNotification)
{
let contentInsets : UIEdgeInsets = UIEdgeInsets.zero
scrollViewGroupMakeTakers.contentInset = contentInsets;
scrollViewGroupMakeTakers.scrollIndicatorInsets = contentInsets;
}

关于ios - 当键盘出现时,如何将最后一个文本字段的 View 向上移动?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41120987/

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