Ios 更新帧时,UITextView跳到屏幕底部,位于键盘后面
我正在实现一个UITextView textInput,其效果与iOS消息窗口类似,其中textview根据文本内容展开/收缩 当我在textViewDidChange函数中更新UITextView框架时,UITextView跳到屏幕底部,隐藏在键盘后面。它不会出现在指定的帧中 这是我的密码Ios 更新帧时,UITextView跳到屏幕底部,位于键盘后面,ios,keyboard,uitextview,Ios,Keyboard,Uitextview,我正在实现一个UITextView textInput,其效果与iOS消息窗口类似,其中textview根据文本内容展开/收缩 当我在textViewDidChange函数中更新UITextView框架时,UITextView跳到屏幕底部,隐藏在键盘后面。它不会出现在指定的帧中 这是我的密码 func textViewDidChange(textView: UITextView) { print ("textview did changed") let fixedWidth
func textViewDidChange(textView: UITextView)
{
print ("textview did changed")
let fixedWidth : CGFloat = textView.frame.size.width
let newSize : CGSize = textView.sizeThatFits(CGSizeMake(fixedWidth, CGFloat(MAXFLOAT)))
var newFrame : CGRect = textView.frame
newFrame.size = CGSizeMake(CGFloat(fmaxf((Float)(newSize.width), (Float)(fixedWidth))),newSize.height)
textView.frame = newFrame
}
另外,这里还有keyboardWillAppear()和keyboardWillAppear()函数,它们工作正常
func keyboardWillAppear(notification: NSNotification){
print ("keyboard will appear")
let userInfo:NSDictionary = notification.userInfo!
let keyboardSize:CGSize = userInfo.objectForKey(UIKeyboardFrameBeginUserInfoKey)!.CGRectValue().size
let contentInsets:UIEdgeInsets = UIEdgeInsetsMake(0, 0, keyboardSize.height, 0)
self.tableView.contentInset = contentInsets
self.tableView.scrollIndicatorInsets = contentInsets
scrollToBottomOfTheTable()
var messageFrame:CGRect = self.textInput.frame
messageFrame.origin.y -= keyboardSize.height
self.textInput.frame = messageFrame
}
func keyboardWillDisappear(notification: NSNotification) {
print ("keyboard will disappear")
let userInfo:NSDictionary = notification.userInfo!
let keyboardSize:CGSize = userInfo.objectForKey(UIKeyboardFrameBeginUserInfoKey)!.CGRectValue().size
UIView.beginAnimations(nil, context: nil)
UIView.setAnimationDuration(0.25)
self.tableView.contentInset = UIEdgeInsetsZero
UIView.commitAnimations()
self.tableView.scrollIndicatorInsets = UIEdgeInsetsZero
var messageFrame:CGRect = self.textInput.frame
messageFrame.origin.y += keyboardSize.height
self.textInput.frame = messageFrame
}
请帮忙!谢谢。您正在使用自动布局吗?不,我没有。没有xib文件,我正在swift文件中构建所有ui元素。