根据 UITextView 的大小向上移动子视图 swift
Move up subviews based on size of UITextView swift
我有一个 UITextView,它会在用户输入多行文本时改变高度。我的问题是,当用户输入足够多的行时,UITextView 会被位于 textview 上方的 UITableView 覆盖。当 textview 的高度增加时,有什么方法可以通过编程方式向上移动 tableview 吗?
这就是改变 textview 高度的原因,它位于 textViewDidChange 函数中:
if (self.contentSize.height > self.frame.height && self.contentSize.height < 166){
self.frame.origin.y -= (self.contentSize.height - self.frame.height)
self.frame.size = self.contentSize
}else if (self.contentSize.height < self.frame.height){
self.frame.origin.y += self.frame.height - self.contentSize.height
self.frame.size = self.contentSize
}
如果您使用故事板,只需使用 NSLayoutConstraints 即可轻松完成此操作,您也可以在代码中执行此操作,但在 stroyboard 中会更容易。如果 textview 位于底部,您可以按住 Ctrl 键从它拖动到 tablview 并使用垂直间距。对于 iMessage 样式的文本视图,使用将文本视图固定到具有布局约束的 VC 视图的底部,并将其顶部固定到表格视图的底部。您必须在 VC 中注册 UIKeyBoardWillHide 和 UIKeyboardWillShow。然后实现一个像底部的方法。您可以在 viewDidAppear 中添加观察者,并在 viewWillDisappear 中移除它们。以下方法中的height变量是指我在textView的高度上设置的约束,bottomConstraint是我在storyboard中设置的textView的bottomConstraint的IBOutlet。
`
func keyboardToggle(通知:NSNotification){
let info:AnyObject = notification.userInfo!
let keyboardHeight = (info[UIKeyboardFrameEndUserInfoKey] as NSValue).CGRectValue().height
let duration = info[UIKeyboardAnimationDurationUserInfoKey] as Double
var height:CGFloat!
var botConst: CGFloat!
switch notification.name {
case UIKeyboardWillShowNotification:
height = textView.contentSize.height
botConst = keyboardHeight
case UIKeyboardWillHideNotification:
height = 40.0
botConst = 0.0
characterCountLabel.hidden = true
bottomConstraint.constant = botConst
view.layoutIfNeeded()
delegate.view.layoutIfNeeded()
UIView.animateWithDuration(duration, animations: { () -> Void in
self.view.layoutIfNeeded()
self.delegate.view.layoutIfNeeded()
})
`
我最终采用了与上述答案略有不同的方法。我使用 NSNotificationCenter 发送文本已输入文本视图的通知,然后我更新了文本视图的大小并更新了表格视图的 y 坐标。
这是更新我的文本视图和表格视图位置的函数:
func keyPressed(sender: NSNotification) {
if let text = textView{
if let messages = messageViews{
if (text.contentSize.height > text.frame.height){
messages.frame.origin.y -= (text.contentSize.height - text.frame.height)
text.frame.origin.y -= (text.contentSize.height - text.frame.height)
text.frame.size = text.contentSize
}else if(text.contentSize.height < text.frame.height){
messages.frame.origin.y += (text.frame.height - text.contentSize.height)
text.frame.origin.y += (text.frame.height - text.contentSize.height)
text.frame.size = text.contentSize
}
}
}
}
我有一个 UITextView,它会在用户输入多行文本时改变高度。我的问题是,当用户输入足够多的行时,UITextView 会被位于 textview 上方的 UITableView 覆盖。当 textview 的高度增加时,有什么方法可以通过编程方式向上移动 tableview 吗?
这就是改变 textview 高度的原因,它位于 textViewDidChange 函数中:
if (self.contentSize.height > self.frame.height && self.contentSize.height < 166){
self.frame.origin.y -= (self.contentSize.height - self.frame.height)
self.frame.size = self.contentSize
}else if (self.contentSize.height < self.frame.height){
self.frame.origin.y += self.frame.height - self.contentSize.height
self.frame.size = self.contentSize
}
如果您使用故事板,只需使用 NSLayoutConstraints 即可轻松完成此操作,您也可以在代码中执行此操作,但在 stroyboard 中会更容易。如果 textview 位于底部,您可以按住 Ctrl 键从它拖动到 tablview 并使用垂直间距。对于 iMessage 样式的文本视图,使用将文本视图固定到具有布局约束的 VC 视图的底部,并将其顶部固定到表格视图的底部。您必须在 VC 中注册 UIKeyBoardWillHide 和 UIKeyboardWillShow。然后实现一个像底部的方法。您可以在 viewDidAppear 中添加观察者,并在 viewWillDisappear 中移除它们。以下方法中的height变量是指我在textView的高度上设置的约束,bottomConstraint是我在storyboard中设置的textView的bottomConstraint的IBOutlet。
` func keyboardToggle(通知:NSNotification){
let info:AnyObject = notification.userInfo!
let keyboardHeight = (info[UIKeyboardFrameEndUserInfoKey] as NSValue).CGRectValue().height
let duration = info[UIKeyboardAnimationDurationUserInfoKey] as Double
var height:CGFloat!
var botConst: CGFloat!
switch notification.name {
case UIKeyboardWillShowNotification:
height = textView.contentSize.height
botConst = keyboardHeight
case UIKeyboardWillHideNotification:
height = 40.0
botConst = 0.0
characterCountLabel.hidden = true
bottomConstraint.constant = botConst
view.layoutIfNeeded()
delegate.view.layoutIfNeeded()
UIView.animateWithDuration(duration, animations: { () -> Void in
self.view.layoutIfNeeded()
self.delegate.view.layoutIfNeeded()
})
`
我最终采用了与上述答案略有不同的方法。我使用 NSNotificationCenter 发送文本已输入文本视图的通知,然后我更新了文本视图的大小并更新了表格视图的 y 坐标。
这是更新我的文本视图和表格视图位置的函数:
func keyPressed(sender: NSNotification) {
if let text = textView{
if let messages = messageViews{
if (text.contentSize.height > text.frame.height){
messages.frame.origin.y -= (text.contentSize.height - text.frame.height)
text.frame.origin.y -= (text.contentSize.height - text.frame.height)
text.frame.size = text.contentSize
}else if(text.contentSize.height < text.frame.height){
messages.frame.origin.y += (text.frame.height - text.contentSize.height)
text.frame.origin.y += (text.frame.height - text.contentSize.height)
text.frame.size = text.contentSize
}
}
}
}