我有一個不與主視圖具有相同高度的uiscrollview。我在這個滾動視圖之外有一個uitextfield和一個按鈕。我在那裏輸入一個值,然後點擊按鈕,它會在滾動視圖內創建另一個文本框,然後如果再按一次,它將在先前創建的文本框下面創建另一個文本框。現在,我正在嘗試將視圖滾動到新創建的文本框,該文本框應該在鍵盤視圖上方可見。我已經嘗試了以下。它不工作:Swift:滾動不起作用
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每次增加一個新的文本字段被創建。
沒有得到確切的要求!當你按下按鈕時,它會創建新的文本字段,那麼你是否想要該文本字段作爲活動文本字段(鍵盤是爲它呈現)? – Lion
我不想要活動文本框,因爲活動文本框將始終是我輸入值的位置(位於uiscrollview之外的文本框)。我想要的是滾動視圖滾動到新創建的文本框。 –