【发布时间】:2016-12-31 05:28:12
【问题描述】:
我有一个与主视图高度不同的 uiscrollview。我在这个滚动视图之外有一个 uitextfield 和一个按钮。我在其中输入一个值,然后单击按钮,它会在滚动视图中创建另一个文本字段,然后如果我再次按下,它将在先前创建的文本字段下方创建另一个文本字段。现在,我正在尝试将视图滚动到新创建的文本字段,该文本字段应该在键盘视图上方可见。我尝试了以下方法。它不起作用:
func keyboardWillShow() {
var userInfo = notification.userInfo!
var keyboardFrame:CGRect = (userInfo[UIKeyboardFrameBeginUserInfoKey] as! NSValue).CGRectValue()
let textField: UITextField = self.scrollView!.viewWithTag(rowsCount) as! UITextField
let newRect: CGRect = CGRectMake(textField.frame.origin.x, textField.frame.origin.y, textField.frame.width, textField.frame.height)
let newFrame = self.view.convertRect(newRect, fromView: nil)
keyboardFrame = self.view.convertRect(keyboardFrame, fromView: nil)
// contentInset.bottom = newRect.origin.y + newRect.height
var viewRect: CGRect = self.view.frame
viewRect.size.height = viewRect.height - keyboardFrame.height - 40.0
if (!CGRectContainsPoint(viewRect, (newFrame.origin))) {
self.scrollView.scrollRectToVisible((newFrame), animated: true)
}
}
P.S.:rowsCount 每次创建新文本字段时都会增加。
【问题讨论】:
-
没有得到确切的要求!!当您按下按钮时,它将创建新的文本字段,那么您是否希望该文本字段作为活动文本字段(为其提供键盘)?
-
我不想要活动文本字段,因为活动文本字段将始终是我输入值的那个(在 uiscrollview 之外的那个)。我想要的是滚动视图滚动到新创建的文本字段。
标签: ios swift uiscrollview uitextfield cgrect