gpt4 book ai didi

ios - 当键盘快速出现时滚动 UITextView

转载 作者:可可西里 更新时间:2023-11-01 00:53:21 25 4
gpt4 key购买 nike

我现在面临的问题是UIScrollView中有一个UITextField。当我单击 UITextField 时,我正在显示带有 UIToolBar 的 UIPickerView。当 PickerView 出现时,我需要将 UITextField 向上移动。

这是我的设计 enter image description here

这是我的代码

 func keyboardWillShow(notification: NSNotification)
{
let userInfo = notification.userInfo
if let info = userInfo
{
let animationDurationObject =
info[UIKeyboardAnimationDurationUserInfoKey] as NSValue

let keyboardEndRectObject =
info[UIKeyboardFrameEndUserInfoKey] as NSValue

var animationDuration = 0.0
var keyboardEndRect = CGRectZero

animationDurationObject.getValue(&animationDuration)
keyboardEndRectObject.getValue(&keyboardEndRect)

let window = UIApplication.sharedApplication().keyWindow

keyboardEndRect = view.convertRect(keyboardEndRect, fromView: window)

let intersectionOfKeyboardRectAndWindowRect =
CGRectIntersection(view.frame, keyboardEndRect);

UIView.animateWithDuration(animationDuration, animations: {[weak self] in

self!.scrollView.contentInset = UIEdgeInsets(top: 0,
left: 0,
bottom: intersectionOfKeyboardRectAndWindowRect.size.height,
right: 0)
self?.scrollView.scrollRectToVisible(self!.activeTextField.frame, animated: true)
})
}
}

func keyboardWillHide(notification: NSNotification)
{
let userInfo = notification.userInfo

if let info = userInfo{
let animationDurationObject =
info[UIKeyboardAnimationDurationUserInfoKey]
as NSValue

var animationDuration = 0.0;

animationDurationObject.getValue(&animationDuration)

UIView.animateWithDuration(animationDuration, animations: {
[weak self] in
self?.scrollView.contentInset = UIEdgeInsetsZero
self?.scrollView.scrollIndicatorInsets = UIEdgeInsetsZero
})
}
}

func textFieldShouldReturn(textField: UITextField) -> Bool
{
textField.resignFirstResponder()
return true
}

func textFieldShouldBeginEditing(textField: UITextField) -> Bool
{
var returnVal = true
self.activeTextField = textField
if textField == self.pickerTextField
{
returnVal = false
var yval:CGFloat = self.view.bounds.size.height - 206
self.pickerContainerView.frame = CGRectMake(0, yval, self.view.bounds.size.width, self.pickerContainerView.frame.size.height)
self.pickerContainerView.hidden = false
self.view.addSubview(self.pickerContainerView)
}
return returnVal
}

func textFieldDidEndEditing(textField: UITextField) {
self.activeTextField = nil
textField.resignFirstResponder()
}

当我单击其他 TextField 时,这些 TextField 在出现键盘时上升。但是PickerTextField怎么办

关于 UItextView 的任何想法

谢谢。

最佳答案

swift 3 版本:

func textViewDidBeginEditing(_ textView: UITextView) {
let scrollPoint : CGPoint = CGPoint.init(x:0, y:textView.frame.origin.y)
self.scrollView.setContentOffset(scrollPoint, animated: true)
}

func textViewDidEndEditing(_ textView: UITextView) {
self.scrollView.setContentOffset(CGPoint.zero, animated: true)
}

关于ios - 当键盘快速出现时滚动 UITextView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28847532/

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